Last active
October 4, 2022 12:47
-
-
Save altbdoor/e63d02b63654be51b95a1e5c7ad253fd to your computer and use it in GitHub Desktop.
Manually generating an Angular SCAM
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
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE | |
Version 2, December 2004 | |
Copyright (C) 2004 Sam Hocevar <[email protected]> | |
Everyone is permitted to copy and distribute verbatim or modified | |
copies of this license document, and changing it is allowed as long | |
as the name is changed. | |
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE | |
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION | |
0. You just DO WHAT THE FUCK YOU WANT TO. |
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
#!/usr/bin/env node | |
const { execSync } = require('child_process'); | |
const { readFileSync, writeFileSync, unlinkSync } = require('fs'); | |
const { basename, resolve } = require('path'); | |
const { argv, exit } = require('process'); | |
const rootPath = execSync('npm prefix').toString().trim(); | |
console.log(`(i) assuming root path as ${rootPath}`); | |
const appArgs = argv.slice(2); | |
if (appArgs.length !== 2) { | |
console.error('(!) incorrect args'); | |
console.error('please pass in the desired schematic, and path to file'); | |
console.error('example: npm run scam c src/app/sample/sample-foobar'); | |
exit(1); | |
} | |
const [genType, genPath] = appArgs; | |
const ngGenPath = genPath.replace('src/app/', ''); | |
let command = `npx ng generate module ${ngGenPath} --flat=false` | |
console.log('(i) dry run to capture Angular path') | |
const moduleCreatedLog = execSync(`${command} --dry-run=true`).toString().trim(); | |
const moduleFilePath = moduleCreatedLog.split(' ')[1]; | |
console.log(`(i) captured path "${moduleFilePath}"`); | |
console.log(`(i) running > ${command}`); | |
execSync(command, { stdio: 'inherit' }); | |
command = `npx ng generate ${genType} ${ngGenPath} --flat=false --module=${moduleFilePath.replace('src/app/', '')}` | |
console.log(`(i) running > ${command}`); | |
execSync(command, { stdio: 'inherit' }); | |
const moduleFileContent = readFileSync(moduleFilePath, { encoding: 'utf8' }); | |
let moduleFileNgModule = '@NgModule' + moduleFileContent.split('@NgModule').pop(); | |
moduleFileNgModule = moduleFileNgModule.replace(/( +declarations: \[.+?\],)/s, (match) => | |
[match, match.replace('declarations', 'exports')].join('\n') | |
); | |
const guessFilenameRegex = moduleFileContent.match(/import { .+? } from '\.\/(.+?)';/); | |
if (guessFilenameRegex.length !== 2) { | |
console.error('(!) error guessing generated filename'); | |
console.error('guessed', guessFilenameRegex); | |
exit(1); | |
} | |
const guessFilename = guessFilenameRegex[1]; | |
const genFilePath = resolve(rootPath, moduleFilePath, '..', `${guessFilename}.ts`); | |
const genFileContent = readFileSync(genFilePath, { encoding: 'utf8' }); | |
const finalContent = [ | |
...moduleFileContent.split('\n').filter((line) => line.startsWith('import') && !line.includes(guessFilename)), | |
genFileContent, | |
moduleFileNgModule, | |
]; | |
writeFileSync(genFilePath, finalContent.join('\n')); | |
console.log(`(i) moved module declaration into ${guessFilename}.ts file`); | |
unlinkSync(moduleFilePath); | |
console.log(`(i) deleted unused ${basename(moduleFilePath)} file`); | |
console.log(`(i) done, please optimize the imports manually in ${guessFilename}.ts`); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment