Skip to content

Commit 9746ed2

Browse files
authored
feat (schematics): allow comments in angular.json file (Issue #480) (#550)
* fix(schematics): restore forgotten files * feat(schematics): add npm scripts 'clean' & 'prebuild' * refactor(schematics): remove the last occurrences of 'require' & 'import \* as' * refactor(schematics): unify tree file access functions * refactor(schematics): add a proxy function for JSON.parse * feat(schematics): allow comments in angular.json file ISSUES CLOSED: #480 * fix(schematics): scully-schematics requires js-yaml * refactor(schematics): remove npm scripts 'clean' & 'prebuild'
1 parent f309a8f commit 9746ed2

12 files changed

Lines changed: 267 additions & 120 deletions

File tree

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
title: override-me
2+
thumbnail: assets/images/default.jpg
3+
author: John Doe
4+
mail: John.Doe@example.com
5+
keywords:
6+
- angular
7+
- scully
8+
language: en

libs/scully-schematics/package-lock.json

Lines changed: 62 additions & 32 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import {
2+
DisplayProcessor,
3+
SpecReporter,
4+
StacktraceOption
5+
} from 'jasmine-spec-reporter';
6+
import SuiteInfo = jasmine.SuiteInfo;
7+
import mapping from 'source-map-support';
8+
9+
mapping.install();
10+
11+
const displayStacktrace = StacktraceOption.PRETTY;
12+
13+
class CustomProcessor extends DisplayProcessor {
14+
public displayJasmineStarted(info: SuiteInfo, log: string): string {
15+
return `TypeScript (${displayStacktrace} format) ${log}`;
16+
}
17+
}
18+
19+
jasmine.getEnv().clearReporters();
20+
jasmine.getEnv().addReporter(
21+
new SpecReporter({
22+
spec: {
23+
displayStacktrace
24+
},
25+
customProcessors: [CustomProcessor]
26+
})
27+
);
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"spec_dir": "spec",
3+
"spec_files": ["**/*[sS]pec.js"],
4+
"helpers": ["helpers/**/*.js"],
5+
"stopSpecOnExpectationFailure": false,
6+
"random": true
7+
}

libs/scully-schematics/src/create-markdown/index.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -88,11 +88,7 @@ const addModule = (options: Schema) => (
8888
let prefix = 'app';
8989
let styleFormat = 'css';
9090
if (tree.exists(ANGULAR_CONF_FILE)) {
91-
prefix = getPrefix(
92-
tree,
93-
getFileContents(tree, ANGULAR_CONF_FILE),
94-
options.project
95-
);
91+
prefix = getPrefix(tree, options.project, ANGULAR_CONF_FILE);
9692
addRouteToModule(tree, options);
9793
styleFormat =
9894
getStyle(tree, options.project, ANGULAR_CONF_FILE) || styleFormat;

libs/scully-schematics/src/ng-add/index.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,24 @@ import {
99
NodePackageInstallTask,
1010
RunSchematicTask
1111
} from '@angular-devkit/schematics/tasks';
12-
import * as ts from '@schematics/angular/third_party/github.com/Microsoft/TypeScript/lib/typescript';
12+
import {
13+
createSourceFile,
14+
ScriptTarget
15+
} from '@schematics/angular/third_party/github.com/Microsoft/TypeScript/lib/typescript';
1316
import {
1417
addImportToModule,
1518
insertImport
1619
} from '@schematics/angular/utility/ast-utils';
1720
import { InsertChange } from '@schematics/angular/utility/change';
21+
1822
import { getSourceFile, getSrc } from '../utils/utils';
1923
import {
2024
addPackageToPackageJson,
2125
getPackageVersionFromPackageJson
2226
} from './package-config';
2327
import { Schema } from './schema';
2428
import { scullyComponentVersion, scullyVersion } from './version-names';
29+
2530
export default (options: Schema): Rule => {
2631
return chain([
2732
checkAngularVersion(),
@@ -112,10 +117,10 @@ const addScullyModule = (project: string) => (
112117
throw new SchematicsException(`File ${mainFilePath} does not exist.`);
113118
}
114119
const sourceText = text.toString();
115-
const source = ts.createSourceFile(
120+
const source = createSourceFile(
116121
mainFilePath,
117122
sourceText,
118-
ts.ScriptTarget.Latest,
123+
ScriptTarget.Latest,
119124
true
120125
);
121126
const changes = addImportToModule(

libs/scully-schematics/src/ng-add/index_spec.ts

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
import { getFileContent } from '@schematics/angular/utility/test';
66
import { join } from 'path';
77

8+
import { getPackageJson } from '../utils/utils';
89
import { setupProject } from '../utils/test-utils';
910
import { Schema as ModelSchema } from './schema';
1011
import { scullyVersion, scullyComponentVersion } from './version-names';
@@ -47,19 +48,7 @@ describe('ng-add schematic', () => {
4748
});
4849

4950
it('should add dependencies', () => {
50-
const packageJson = JSON.parse(
51-
getFileContent(appTree, PACKAGE_JSON_PATH)
52-
);
53-
const { dependencies } = packageJson;
54-
expect(appTree.files).toContain(PACKAGE_JSON_PATH);
55-
expect(dependencies['@scullyio/ng-lib']).toEqual(scullyVersion);
56-
expect(dependencies['@scullyio/scully']).toEqual(scullyComponentVersion);
57-
});
58-
59-
it('should add dependencies', () => {
60-
const packageJson = JSON.parse(
61-
getFileContent(appTree, PACKAGE_JSON_PATH)
62-
);
51+
const packageJson = getPackageJson(appTree);
6352
const { dependencies } = packageJson;
6453
expect(appTree.files).toContain(PACKAGE_JSON_PATH);
6554
expect(dependencies['@scullyio/ng-lib']).toEqual(scullyVersion);

libs/scully-schematics/src/scully/index_spec.ts

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { getFileContent } from '@schematics/angular/utility/test';
66
import { join } from 'path';
77

88
import { setupProject } from '../utils/test-utils';
9+
import { getPackageJson } from '../utils/utils';
910

1011
const schematicCollectionPath = join(
1112
__dirname,
@@ -33,6 +34,8 @@ exports.config = {
3334
};
3435
`;
3536

37+
const ANGULAR_CONF_PATH = '/angular.json';
38+
3639
const defaultOptions = Object.freeze({
3740
project: 'defaultProject'
3841
});
@@ -83,9 +86,7 @@ describe('scully schematic', () => {
8386

8487
it(`should modify the 'package.json'`, () => {
8588
expect(appTree.files).toContain(PACKAGE_JSON_PATH);
86-
const packageJson = JSON.parse(
87-
getFileContent(appTree, PACKAGE_JSON_PATH)
88-
);
89+
const packageJson = getPackageJson(appTree);
8990
const { scripts } = packageJson;
9091
expect(scripts.scully).toEqual('scully');
9192
expect(scripts['scully:serve']).toEqual('scully serve');
@@ -103,4 +104,24 @@ describe('scully schematic', () => {
103104
expect(getFileContent(appTree, SCULLY_PATH)).toEqual('foo');
104105
});
105106
});
107+
108+
describe('when the angular.json contains any comment', () => {
109+
it('should deal with it', async () => {
110+
const options = { ...defaultOptions };
111+
const angularConfigContent = getFileContent(appTree, ANGULAR_CONF_PATH);
112+
const angularConfigLines = angularConfigContent.split('\n');
113+
angularConfigLines[3] += ` // dummy comment`;
114+
appTree.overwrite(ANGULAR_CONF_PATH, angularConfigLines.join('\n'));
115+
const NO_ERROR = '';
116+
let error = NO_ERROR;
117+
try {
118+
await customRunner
119+
.runSchematicAsync('scully', options, appTree)
120+
.toPromise();
121+
} catch (e) {
122+
error = e;
123+
}
124+
expect(error).toEqual(NO_ERROR);
125+
});
126+
});
106127
});

0 commit comments

Comments
 (0)