-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
96 additions
and
78 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
const tokenizer = (st: string) => { | ||
let start = 0; | ||
let pos = 0; | ||
const arr = []; | ||
while (pos != st.length) { | ||
if (st[pos] == ",") { | ||
while (st[pos] != "\n" && pos != st.length) { | ||
pos++; | ||
} | ||
|
||
arr.push(st.substring(start, pos)); | ||
start = pos; | ||
} | ||
pos++; | ||
} | ||
|
||
if (start != pos) { | ||
arr.push(st.substring(start, pos)); | ||
} | ||
|
||
return arr.filter(x => x.trim().length > 0); | ||
} | ||
|
||
const addToType = (data: string, type: string, str: string) => { | ||
const stInx = data.indexOf(`@NgModule(`); | ||
const typeIndex = data.indexOf(type, stInx); | ||
|
||
if (typeIndex == -1) { | ||
const startIndex = data.lastIndexOf("]"); | ||
const tmpl = ` | ||
${type}: [ | ||
"${str}" | ||
]`; | ||
|
||
return data.substring(0, startIndex + 1) + "," + tmpl + data.substring(startIndex + 1, data.length); | ||
} | ||
|
||
const startIndex = data.indexOf("[", typeIndex); | ||
const endIndex = data.indexOf("]", startIndex); | ||
|
||
const area = data.substring(startIndex + 1, endIndex); | ||
const lines = tokenizer(area); | ||
|
||
let last = lines.length > 0 ? lines.pop().trimEnd() : ""; | ||
let commentsIndex = last.indexOf("//"); | ||
let injectIndex = (commentsIndex == -1) ? last.length - 1 : commentsIndex - 1; | ||
|
||
if (commentsIndex != -1) { | ||
while (last[injectIndex] == " ") { | ||
injectIndex--; | ||
} | ||
} | ||
|
||
if (last[injectIndex] != "," && last.length > 0) { | ||
last = last.substring(0, injectIndex + 1) + "," + last.substring(injectIndex + 1, last.length); | ||
} | ||
// add last line | ||
lines.push(last); | ||
|
||
// inject new token | ||
lines.push("\n " + str); | ||
|
||
const newArea = "[\t" + lines.join('') + "\n ]"; | ||
|
||
const newStr = data.substring(0, startIndex) + newArea + data.substring(endIndex + 1, data.length); | ||
|
||
return newStr; | ||
} | ||
|
||
const addToImport = (data: string, str: string) => { | ||
const lastImportInx = data.lastIndexOf('import '); | ||
const endOfLastImportInx = data.indexOf('\n', lastImportInx); | ||
const fileLength = data.length; | ||
return data.substring(0, endOfLastImportInx) + `\n${str}` + data.substring(endOfLastImportInx, fileLength); | ||
} | ||
|
||
export { | ||
addToType, | ||
addToImport | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters