Skip to content
This repository was archived by the owner on Apr 22, 2020. It is now read-only.

Commit d73f8ac

Browse files
committed
build: use Prettier for automated code formatting
1 parent 630d184 commit d73f8ac

15 files changed

+126
-92
lines changed

.editorconfig

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# http://editorconfig.org
2+
3+
root = true
4+
5+
[*]
6+
charset = utf-8
7+
indent_style = space
8+
indent_size = 2
9+
insert_final_newline = true
10+
trim_trailing_whitespace = true
11+
12+
[*.md]
13+
insert_final_newline = false
14+
trim_trailing_whitespace = false

.prettierrc

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
{
2+
"singleQuote": true,
23
"trailingComma": "es5"
34
}

jest.config.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
module.exports = {
2-
preset: "ts-jest",
3-
testEnvironment: "node",
4-
rootDir: "src/"
2+
preset: 'ts-jest',
3+
testEnvironment: 'node',
4+
rootDir: 'src/',
55
};

package.json

+4-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,9 @@
2626
"lint": "tslint -p tsconfig.json",
2727
"clean": "rm -Rf dist",
2828
"build": "yarn clean && mkdir -p dist && node scripts/create_package.js && tsc -p tsconfig.json",
29-
"ci": "yarn lint && yarn build && yarn test"
29+
"check-format": "prettier --check src/**/*.ts scripts/*.js",
30+
"format": "prettier --write src/**/*.ts scripts/*.js",
31+
"ci": "yarn check-format && yarn lint && yarn build && yarn test"
3032
},
3133
"dependencies": {
3234
"@angular-devkit/core": "^7.0.6",
@@ -41,6 +43,7 @@
4143
"@commitlint/config-angular": "8.3.4",
4244
"husky": "4.0.7",
4345
"jest": "23.6.0",
46+
"prettier": "^2.0.4",
4447
"ts-jest": "23.10.5",
4548
"tslint": "^6.1.1"
4649
}

scripts/create_package.js

+28-28
Original file line numberDiff line numberDiff line change
@@ -1,67 +1,67 @@
1-
const fs = require("fs");
2-
const path = require("path");
1+
const fs = require('fs');
2+
const path = require('path');
33

44
function cp(source, dest) {
55
dest = path.resolve(dest);
66
!fs.existsSync(path.dirname(dest)) && fs.mkdirSync(path.dirname(dest));
77
fs.writeFileSync(
88
dest,
9-
fs.readFileSync(require.resolve(source), "utf-8"),
10-
"utf-8"
9+
fs.readFileSync(require.resolve(source), 'utf-8'),
10+
'utf-8'
1111
);
1212
}
1313

1414
// Copying schemas
15-
cp("../src/collection.json", "dist/collection.json");
16-
cp("../src/ng-add/schema.json", "dist/ng-add/schema.json");
15+
cp('../src/collection.json', 'dist/collection.json');
16+
cp('../src/ng-add/schema.json', 'dist/ng-add/schema.json');
1717

1818
// read oryginal package .json
19-
const pkgJson = require("../package.json");
19+
const pkgJson = require('../package.json');
2020

2121
const targetPkgJson = {};
2222

2323
// copy some of the package.json fields from src to dest
2424
[
25-
"name",
26-
"version",
27-
"description",
28-
"keywords",
29-
"author",
30-
"repository",
31-
"license",
32-
"bugs",
33-
"homepage",
34-
].forEach(function(field) {
25+
'name',
26+
'version',
27+
'description',
28+
'keywords',
29+
'author',
30+
'repository',
31+
'license',
32+
'bugs',
33+
'homepage',
34+
].forEach(function (field) {
3535
targetPkgJson[field] = pkgJson[field];
3636
});
3737

3838
// add dependencies (use the same versions as in the oryginal package.json)
3939
targetPkgJson.dependencies = {};
4040
[
41-
"@angular-devkit/core",
42-
"@angular-devkit/schematics",
43-
"@schematics/angular",
44-
"typescript",
45-
].forEach(function(depPkg) {
41+
'@angular-devkit/core',
42+
'@angular-devkit/schematics',
43+
'@schematics/angular',
44+
'typescript',
45+
].forEach(function (depPkg) {
4646
targetPkgJson.dependencies[depPkg] = pkgJson.dependencies[depPkg];
4747
});
4848

4949
// add schematics entry
50-
targetPkgJson["schematics"] = "./collection.json";
50+
targetPkgJson['schematics'] = './collection.json';
5151

5252
// add keywords (https://twitter.com/stephenfluin/status/981979735839277056)
5353
targetPkgJson.keywords = Array.from(
5454
new Set([
5555
...(targetPkgJson.keywords || []),
56-
"angular-cli",
57-
"schematics",
58-
"ng-add",
56+
'angular-cli',
57+
'schematics',
58+
'ng-add',
5959
])
6060
);
6161

6262
// write down resulting package.json
6363
fs.writeFileSync(
64-
"dist/package.json",
64+
'dist/package.json',
6565
JSON.stringify(targetPkgJson, null, 2),
66-
"utf-8"
66+
'utf-8'
6767
);

scripts/preinstall.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
if (process.env.npm_execpath.indexOf('yarn') === -1) {
22
throw new Error(`
3-
3+
44
###################################################
55
# #
66
# Please use Yarn to install dependencies #

src/ng-add/index.spec.ts

+8-8
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,24 @@ import { SchematicTestRunner } from '@angular-devkit/schematics/testing';
33

44
import { createTestApp, getFileContent } from '../utils/testing';
55

6-
describe("NgBootstrap schematics", () => {
6+
describe('NgBootstrap schematics', () => {
77
let runner: SchematicTestRunner;
88
let appTree: Tree;
99

1010
beforeEach(() => {
1111
runner = new SchematicTestRunner(
12-
"schematics",
13-
require.resolve("../collection.json")
12+
'schematics',
13+
require.resolve('../collection.json')
1414
);
1515
appTree = createTestApp(runner);
1616
});
1717

18-
it("should update package.json", () => {
19-
const tree = runner.runSchematic("ng-add", {}, appTree);
20-
const packageJson = JSON.parse(getFileContent(tree, "/package.json"));
18+
it('should update package.json', () => {
19+
const tree = runner.runSchematic('ng-add', {}, appTree);
20+
const packageJson = JSON.parse(getFileContent(tree, '/package.json'));
2121
const dependencies = packageJson.dependencies;
2222

23-
expect(dependencies["@ng-bootstrap/ng-bootstrap"]).toBeDefined();
24-
expect(dependencies["bootstrap"]).toBeDefined();
23+
expect(dependencies['@ng-bootstrap/ng-bootstrap']).toBeDefined();
24+
expect(dependencies['bootstrap']).toBeDefined();
2525
});
2626
});

src/ng-add/index.ts

+9-6
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
import { Rule, SchematicContext, Tree } from '@angular-devkit/schematics';
2-
import { NodePackageInstallTask, RunSchematicTask } from '@angular-devkit/schematics/tasks';
2+
import {
3+
NodePackageInstallTask,
4+
RunSchematicTask,
5+
} from '@angular-devkit/schematics/tasks';
36

47
import { addPackageToPackageJson } from '../utils/package-config';
58
import { Schema } from './schema';
69

7-
const NG_BOOTSTRAP_VERSION = "4.0.0";
8-
const BOOTSTRAP_VERSION = "4.0.0";
10+
const NG_BOOTSTRAP_VERSION = '4.0.0';
11+
const BOOTSTRAP_VERSION = '4.0.0';
912

1013
/**
1114
* Schematic factory entry-point for the `ng-add` schematic. The ng-add schematic will be
@@ -15,13 +18,13 @@ export default function ngAdd(options: Schema): Rule {
1518
return (host: Tree, context: SchematicContext) => {
1619
addPackageToPackageJson(
1720
host,
18-
"@ng-bootstrap/ng-bootstrap",
21+
'@ng-bootstrap/ng-bootstrap',
1922
`^${NG_BOOTSTRAP_VERSION}`
2023
);
2124

22-
addPackageToPackageJson(host, "bootstrap", `^${BOOTSTRAP_VERSION}`);
25+
addPackageToPackageJson(host, 'bootstrap', `^${BOOTSTRAP_VERSION}`);
2326

24-
context.addTask(new RunSchematicTask("ng-add-setup-project", options), [
27+
context.addTask(new RunSchematicTask('ng-add-setup-project', options), [
2528
context.addTask(new NodePackageInstallTask()),
2629
]);
2730
};

src/ng-add/setup-project.spec.ts

+10-10
Original file line numberDiff line numberDiff line change
@@ -7,33 +7,33 @@ import { getProjectStyleFile } from '../utils/project-style';
77
import { getProjectTargetOptions } from '../utils/project-targets';
88
import { createTestApp } from '../utils/testing';
99

10-
describe("NgBootstrap schematics", () => {
10+
describe('NgBootstrap schematics', () => {
1111
let runner: SchematicTestRunner;
1212
let appTree: Tree;
1313

1414
beforeEach(() => {
1515
runner = new SchematicTestRunner(
16-
"schematics",
17-
require.resolve("../collection.json")
16+
'schematics',
17+
require.resolve('../collection.json')
1818
);
1919
});
2020

21-
it("should add Bootstrap CSS file on default CLI app", () => {
21+
it('should add Bootstrap CSS file on default CLI app', () => {
2222
appTree = createTestApp(runner);
23-
const tree = runner.runSchematic("ng-add-setup-project", {}, appTree);
23+
const tree = runner.runSchematic('ng-add-setup-project', {}, appTree);
2424
const workspace = getWorkspace(tree);
2525
const project = getProject(workspace, workspace.defaultProject!);
26-
const targetOptions = getProjectTargetOptions(project, "build");
26+
const targetOptions = getProjectTargetOptions(project, 'build');
2727

2828
expect(targetOptions.styles).toContain(
29-
"node_modules/bootstrap/dist/css/bootstrap.css"
29+
'node_modules/bootstrap/dist/css/bootstrap.css'
3030
);
3131
});
3232

33-
it("should setup Bootstrap SCSS on CLI app setup with Sass/SCSS", () => {
34-
appTree = createTestApp(runner, { style: "scss" });
33+
it('should setup Bootstrap SCSS on CLI app setup with Sass/SCSS', () => {
34+
appTree = createTestApp(runner, { style: 'scss' });
3535

36-
const tree = runner.runSchematic("ng-add-setup-project", {}, appTree);
36+
const tree = runner.runSchematic('ng-add-setup-project', {}, appTree);
3737
const workspace = getWorkspace(tree);
3838
const project = getProject(workspace, workspace.defaultProject!);
3939

src/ng-add/setup-project.ts

+19-14
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
1-
import { chain, Rule, SchematicsException, Tree } from '@angular-devkit/schematics';
1+
import {
2+
chain,
3+
Rule,
4+
SchematicsException,
5+
Tree,
6+
} from '@angular-devkit/schematics';
27
import { addImportToModule } from '@schematics/angular/utility/ast-utils';
38
import { InsertChange } from '@schematics/angular/utility/change';
49
import { getWorkspace } from '@schematics/angular/utility/config';
@@ -11,9 +16,9 @@ import { getProjectStyleFile } from '../utils/project-style';
1116
import { getProjectTargetOptions } from '../utils/project-targets';
1217
import { Schema } from './schema';
1318

14-
const NG_BOOTSTRAP_MODULE_NAME = "NgbModule";
15-
const NG_BOOTSTRAP_PACKAGE_NAME = "@ng-bootstrap/ng-bootstrap";
16-
const BOOTSTRAP_CSS_FILEPATH = "node_modules/bootstrap/dist/css/bootstrap.css";
19+
const NG_BOOTSTRAP_MODULE_NAME = 'NgbModule';
20+
const NG_BOOTSTRAP_PACKAGE_NAME = '@ng-bootstrap/ng-bootstrap';
21+
const BOOTSTRAP_CSS_FILEPATH = 'node_modules/bootstrap/dist/css/bootstrap.css';
1722
const BOOTSTRAP_STYLE_IMPORT = `
1823
/* Importing Bootstrap SCSS file. */
1924
@import '~bootstrap/scss/bootstrap';
@@ -26,7 +31,7 @@ function addNgBootstrapModuleToAppModule(options: Schema) {
2631
workspace,
2732
options.project || workspace.defaultProject!
2833
);
29-
const buildOptions = getProjectTargetOptions(project, "build");
34+
const buildOptions = getProjectTargetOptions(project, 'build');
3035

3136
const modulePath = getAppModulePath(host, buildOptions.main);
3237

@@ -37,7 +42,7 @@ function addNgBootstrapModuleToAppModule(options: Schema) {
3742

3843
const source = ts.createSourceFile(
3944
modulePath,
40-
text.toString("utf-8"),
45+
text.toString('utf-8'),
4146
ts.ScriptTarget.Latest,
4247
true
4348
);
@@ -74,13 +79,13 @@ function addBootstrapStyle(options: Schema) {
7479
const schematicsConfig = project.schematics;
7580
if (
7681
schematicsConfig &&
77-
schematicsConfig["@schematics/angular:component"]
82+
schematicsConfig['@schematics/angular:component']
7883
) {
79-
const { styleext } = schematicsConfig["@schematics/angular:component"];
84+
const { styleext } = schematicsConfig['@schematics/angular:component'];
8085
useBootstrapSCSS =
8186
styleext &&
8287
`.${styleext}` === path.extname(styleFilePath) &&
83-
styleext === "scss";
88+
styleext === 'scss';
8489
}
8590

8691
if (useBootstrapSCSS) {
@@ -94,7 +99,7 @@ function addBootstrapStyle(options: Schema) {
9499
}
95100

96101
function injectBootstrapIntoStyleFile(host: Tree, styleFilePath: string) {
97-
const styleContent = host.read(styleFilePath)!.toString("utf-8");
102+
const styleContent = host.read(styleFilePath)!.toString('utf-8');
98103

99104
const recorder = host.beginUpdate(styleFilePath);
100105
recorder.insertRight(styleContent.length, BOOTSTRAP_STYLE_IMPORT);
@@ -108,12 +113,12 @@ function addCSSFileToTarget(options: Schema, host: Tree, assetPath: string) {
108113
workspace,
109114
options.project || workspace.defaultProject!
110115
);
111-
const targetOptions = getProjectTargetOptions(project, "build");
116+
const targetOptions = getProjectTargetOptions(project, 'build');
112117
if (!targetOptions.styles) {
113118
targetOptions.styles = [assetPath];
114119
} else {
115-
const existingStyles = targetOptions.styles.map(s =>
116-
typeof s === "string" ? s : s.input
120+
const existingStyles = targetOptions.styles.map((s) =>
121+
typeof s === 'string' ? s : s.input
117122
);
118123
for (const [, stylePath] of existingStyles.entries()) {
119124
// If the given asset is already specified in the styles, we don't need to do anything.
@@ -123,7 +128,7 @@ function addCSSFileToTarget(options: Schema, host: Tree, assetPath: string) {
123128
}
124129
targetOptions.styles.unshift(assetPath);
125130
}
126-
host.overwrite("angular.json", JSON.stringify(workspace, null, 2));
131+
host.overwrite('angular.json', JSON.stringify(workspace, null, 2));
127132
}
128133

129134
/**

src/utils/package-config.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ export function addPackageToPackageJson(
1616
pkg: string,
1717
version: string
1818
): Tree {
19-
if (host.exists("package.json")) {
20-
const sourceText = host.read("package.json")!.toString("utf-8");
19+
if (host.exists('package.json')) {
20+
const sourceText = host.read('package.json')!.toString('utf-8');
2121
const json = JSON.parse(sourceText);
2222

2323
if (!json.dependencies) {
@@ -29,7 +29,7 @@ export function addPackageToPackageJson(
2929
json.dependencies = sortObjectByKeys(json.dependencies);
3030
}
3131

32-
host.overwrite("package.json", JSON.stringify(json, null, 2));
32+
host.overwrite('package.json', JSON.stringify(json, null, 2));
3333
}
3434

3535
return host;
@@ -40,11 +40,11 @@ export function getPackageVersionFromPackageJson(
4040
tree: Tree,
4141
name: string
4242
): string | null {
43-
if (!tree.exists("package.json")) {
43+
if (!tree.exists('package.json')) {
4444
return null;
4545
}
4646

47-
const packageJson = JSON.parse(tree.read("package.json")!.toString("utf8"));
47+
const packageJson = JSON.parse(tree.read('package.json')!.toString('utf8'));
4848

4949
if (packageJson.dependencies && packageJson.dependencies[name]) {
5050
return packageJson.dependencies[name];

0 commit comments

Comments
 (0)