-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(schematics): add format:check and format:write commands
- Loading branch information
Showing
12 changed files
with
221 additions
and
105 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
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,114 @@ | ||
import { newApp, newLib, newProject, readFile, runCLI, runCommand, updateFile } from '../utils'; | ||
|
||
describe('Command line', () => { | ||
it( | ||
'lint should ensure module boundaries', | ||
() => { | ||
newProject(); | ||
newApp('myapp'); | ||
newLib('mylib'); | ||
newLib('lazylib'); | ||
|
||
const tslint = JSON.parse(readFile('tslint.json')); | ||
tslint.rules['nx-enforce-module-boundaries'][1].lazyLoad.push('lazylib'); | ||
updateFile('tslint.json', JSON.stringify(tslint, null, 2)); | ||
|
||
updateFile( | ||
'apps/myapp/src/main.ts', | ||
` | ||
import '../../../libs/mylib'; | ||
import '@nrwl/lazylib'; | ||
import '@nrwl/mylib/deep'; | ||
` | ||
); | ||
|
||
const out = runCLI('lint --type-check', { silenceError: true }); | ||
expect(out).toContain('library imports must start with @nrwl/'); | ||
expect(out).toContain('import of lazy-loaded libraries are forbidden'); | ||
expect(out).toContain('deep imports into libraries are forbidden'); | ||
}, | ||
1000000 | ||
); | ||
|
||
it( | ||
'nx-migrate should run migrations', | ||
() => { | ||
newProject(); | ||
updateFile( | ||
'node_modules/@nrwl/schematics/migrations/20200101-test-migration.js', | ||
` | ||
exports.default = { | ||
description: 'Test migration', | ||
run: function() { | ||
console.log('Running test migration'); | ||
} | ||
}; | ||
` | ||
); | ||
const out = runCommand('npm run nx-migrate'); | ||
expect(out).toContain('Test migration'); | ||
expect(out).toContain('Running test migration'); | ||
expect(out).toContain('All migrations run successfully'); | ||
|
||
expect(runCommand('npm run nx-migrate')).toContain('No migrations to run'); | ||
}, | ||
1000000 | ||
); | ||
|
||
it( | ||
'affected should print, build, and test affected apps', | ||
() => { | ||
newProject(); | ||
newApp('myapp'); | ||
newApp('myapp2'); | ||
newLib('mylib'); | ||
|
||
updateFile('apps/myapp/src/app/app.component.spec.ts', `import '@nrwl/mylib';`); | ||
|
||
updateRunAffectedToWorkInE2ESetup(); | ||
|
||
const affectedApps = runCommand('npm run affected:apps -- --files="libs/mylib/index.ts"'); | ||
expect(affectedApps).toContain('myapp'); | ||
expect(affectedApps).not.toContain('myapp2'); | ||
|
||
const build = runCommand('npm run affected:build -- --files="libs/mylib/index.ts"'); | ||
expect(build).toContain('Building myapp'); | ||
|
||
const e2e = runCommand('npm run affected:e2e -- --files="libs/mylib/index.ts"'); | ||
expect(e2e).toContain('should display welcome message'); | ||
}, | ||
1000000 | ||
); | ||
|
||
it( | ||
'format should check and reformat the code', | ||
() => { | ||
newProject(); | ||
newApp('myapp'); | ||
updateFile( | ||
'apps/myapp/src/main.ts', | ||
` | ||
const x = 3232; | ||
` | ||
); | ||
|
||
try { | ||
runCommand('npm run -s format:check'); | ||
fail('boom'); | ||
} catch (e) { | ||
expect(e.stdout.toString()).toContain('apps/myapp/src/main.ts'); | ||
} | ||
runCommand('npm run format:write'); | ||
expect(runCommand('npm run -s format:check')).toEqual(''); | ||
}, | ||
1000000 | ||
); | ||
}); | ||
|
||
function updateRunAffectedToWorkInE2ESetup() { | ||
const runAffected = readFile('node_modules/@nrwl/schematics/src/command-line/affected.js'); | ||
const newRunAffected = runAffected | ||
.replace('ng build', '../../node_modules/.bin/ng build') | ||
.replace('ng e2e', '../../node_modules/.bin/ng e2e'); | ||
updateFile('node_modules/@nrwl/schematics/src/command-line/affected.js', newRunAffected); | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
25 changes: 25 additions & 0 deletions
25
packages/schematics/migrations/20180103-update-command-line-scripts.ts
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,25 @@ | ||
import { updateJsonFile } from '../src/collection/utility/fileutils'; | ||
|
||
export default { | ||
description: 'Add format:write and format:check to npm scripts', | ||
run: () => { | ||
updateJsonFile('package.json', json => { | ||
json.scripts = { | ||
...json.scripts, | ||
'apps:affected': 'node ./node_modules/@nrwl/schematics/src/command-line/affected.js apps', | ||
'build:affected': 'node ./node_modules/@nrwl/schematics/src/command-line/affected.js build', | ||
'e2e:affected': 'node ./node_modules/@nrwl/schematics/src/command-line/affected.js e2e', | ||
|
||
'affected:apps': 'node ./node_modules/@nrwl/schematics/src/command-line/affected.js apps', | ||
'affected:build': 'node ./node_modules/@nrwl/schematics/src/command-line/affected.js build', | ||
'affected:e2e': 'node ./node_modules/@nrwl/schematics/src/command-line/affected.js e2e', | ||
|
||
format: 'node ./node_modules/@nrwl/schematics/src/command-line/format.js write', | ||
'format:write': 'node ./node_modules/@nrwl/schematics/src/command-line/format.js write', | ||
'format:check': 'node ./node_modules/@nrwl/schematics/src/command-line/format.js check', | ||
|
||
'nx-migrate': 'node ./node_modules/@nrwl/schematics/src/command-line/nx-migrate.js' | ||
}; | ||
}); | ||
} | ||
}; |
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
2 changes: 1 addition & 1 deletion
2
.../schematics/src/affected/affected.spec.ts → ...cs/src/command-line/affected-apps.spec.ts
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
File renamed without changes.
2 changes: 1 addition & 1 deletion
2
...s/schematics/src/affected/run-affected.ts → ...s/schematics/src/command-line/affected.ts
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,65 @@ | ||
import { execSync } from 'child_process'; | ||
import * as fs from 'fs'; | ||
import * as path from 'path'; | ||
|
||
const command = process.argv[2]; | ||
const files = parseFiles(); | ||
|
||
switch (command) { | ||
case 'write': | ||
write(files); | ||
break; | ||
case 'check': | ||
check(files); | ||
break; | ||
} | ||
|
||
function parseFiles() { | ||
const args = process.argv.slice(3); | ||
if (args.length === 0) { | ||
return '"{apps,libs}/**/*.ts"'; | ||
} | ||
const dashDashFiles = args.filter(a => a.startsWith('--files='))[0]; | ||
if (dashDashFiles) { | ||
args.splice(args.indexOf(dashDashFiles), 1); | ||
return `"${parseDashDashFiles(dashDashFiles).join(',')}"`; | ||
} else { | ||
const withoutShahs = args.slice(2); | ||
return `"${getFilesFromShash(args[0], args[1]).join(',')}"`; | ||
} | ||
} | ||
|
||
function parseDashDashFiles(dashDashFiles: string): string[] { | ||
let f = dashDashFiles.substring(8); // remove --files= | ||
if (f.startsWith('"') || f.startsWith("'")) { | ||
f = f.substring(1, f.length - 1); | ||
} | ||
return f.split(',').map(f => f.trim()); | ||
} | ||
|
||
function getFilesFromShash(sha1: string, sha2: string): string[] { | ||
return execSync(`git diff --name-only ${sha1} ${sha2}`) | ||
.toString('utf-8') | ||
.split('\n') | ||
.map(a => a.trim()) | ||
.filter(a => a.length > 0); | ||
} | ||
|
||
function write(files: string) { | ||
execSync(`node ./node_modules/prettier/bin/prettier.js --single-quote --print-width 120 --write ${files}`, { | ||
stdio: [0, 1, 2] | ||
}); | ||
} | ||
|
||
function check(files: string) { | ||
try { | ||
execSync( | ||
`node ./node_modules/prettier/bin/prettier.js --single-quote --print-width 120 --list-different ${files}`, | ||
{ | ||
stdio: [0, 1, 2] | ||
} | ||
); | ||
} catch (e) { | ||
process.exit(1); | ||
} | ||
} |
File renamed without changes.