Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add format:check and format:write commands #182

Merged
merged 1 commit into from
Jan 2, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 0 additions & 36 deletions e2e/schematics/affected.test.ts

This file was deleted.

114 changes: 114 additions & 0 deletions e2e/schematics/command-line.test.ts
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);
}
29 changes: 0 additions & 29 deletions e2e/schematics/migrator.test.ts

This file was deleted.

32 changes: 0 additions & 32 deletions e2e/schematics/tslint.test.ts

This file was deleted.

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'
};
});
}
};
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,20 @@
"test": "ng test",
"lint": "ng lint",
"e2e": "ng e2e",
"apps:affected": "node ./node_modules/@nrwl/schematics/src/affected/run-affected.js apps",
"build:affected": "node ./node_modules/@nrwl/schematics/src/affected/run-affected.js build",
"e2e:affected": "node ./node_modules/@nrwl/schematics/src/affected/run-affected.js e2e",
"format": "node ./node_modules/prettier/bin/prettier.js --single-quote --print-width 120 --write \"{apps,libs}/**/*.ts\"",
"nx-migrate": "node ./node_modules/@nrwl/schematics/src/migrator/migrator.js"

"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"
},
"private": true,
"dependencies": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ export const angularJsVersion = '1.6.6';
export const ngrxVersion = '4.1.1';
export const nxVersion = '*';
export const schematicsVersion = '*';
export const latestMigration = '20171219-add-affected-commands';
export const latestMigration = '20180103-update-command-line-scripts';
export const prettierVersion = '^1.9.0';
export const typescriptVersion = '2.5.3';
export const rxjsVersion = '^5.5.2';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { affectedApps, dependencies } from './affected';
import { affectedApps, dependencies } from './affected-apps';

describe('Calculates Dependencies Between Apps and Libs', () => {
describe('dependencies', () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { affectedApps } from './affected';
import { affectedApps } from './affected-apps';
import { execSync } from 'child_process';
import * as fs from 'fs';
import * as path from 'path';
Expand Down
65 changes: 65 additions & 0 deletions packages/schematics/src/command-line/format.ts
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);
}
}