Skip to content
This repository has been archived by the owner on Mar 4, 2023. It is now read-only.

Commit

Permalink
Merge pull request #8 from ngstack/prettier
Browse files Browse the repository at this point in the history
support Prettier formatting
  • Loading branch information
DenysVuika authored Oct 9, 2018
2 parents fb5bbd1 + dd002e8 commit 4973d93
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 3 deletions.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,14 @@

Command-line utility for installing Angular libraries into the Angular CLI-based projects.

## Main features

- Install library from the NPM or local package (i.e. tarball file)
- Copy library assets to corresponding application folders
- Register library modules in the application
- Format updated application module with Prettier
- Generate new configuration file for a library

## Getting the tool

Install as a global package using the following command:
Expand All @@ -25,6 +33,7 @@ Run `ngi` without parameters to see the internal help.
| --skip-install | skip installing library |
| --skip-assets | skip copying assets |
| --skip-module | skip module registration |
| --skip-format | skip code formatting |
| -h, --help | output usage information |

## Preparing libraries
Expand Down
11 changes: 11 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,13 @@
"dependencies": {
"chalk": "^2.4.1",
"commander": "^2.18.0",
"prettier": "^1.14.3",
"shelljs": "^0.8.2",
"typescript": "^3.1.1"
},
"devDependencies": {
"@types/node": "^10.11.4",
"@types/prettier": "^1.13.2",
"@types/shelljs": "^0.8.0"
}
}
3 changes: 2 additions & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ program
.option('--skip-install', 'skip installing library')
.option('--skip-assets', 'skip copying assets')
.option('--skip-module', 'skip module registration')
.option('--skip-format', 'skip code formatting')
.action((lib: string, name: string, options: Options) => {
if (options.init) {
createConfig();
Expand Down Expand Up @@ -51,7 +52,7 @@ program
}
if (!options.skipModule) {
console.log(chalk.blue('info'), 'registering modules');
registerModules(moduleName, config);
registerModules(moduleName, config, options.skipFormat);
}
}

Expand Down
1 change: 1 addition & 0 deletions src/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ interface Options {
skipInstall?: boolean;
skipAssets?: boolean;
skipModule?: boolean;
skipFormat?: boolean;
module?: string;
}

Expand Down
17 changes: 15 additions & 2 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import sh from 'shelljs';
import path from 'path';
import fs from 'fs';
import chalk from 'chalk';
import prettier from 'prettier';

import Configuration from './configuration';
import TsUtils from './ts-utils';
Expand Down Expand Up @@ -66,7 +67,8 @@ export function copyAssets(lib: string, config: Configuration): void {

export function registerModules(
moduleName: string,
config: Configuration
config: Configuration,
skipFormat?: boolean
): void {
if (config && config.modules && config.modules.length > 0) {
const modulePath = path.join(
Expand All @@ -82,11 +84,22 @@ export function registerModules(
sourceFile = tsUtils.registerModules(sourceFile, config.modules);

const output = tsUtils.renderFile(sourceFile);
fs.writeFileSync(modulePath, output);
fs.writeFileSync(modulePath, skipFormat ? output : format(output));
}
}

export function version(): string {
const packageInfo = require(path.join(__dirname, '..', 'package.json'));
return packageInfo.version;
}

export function format(source: string): string {
console.log(chalk.blue('info'), `formatting code`);

const options = prettier.resolveConfig.sync(process.cwd()) || {
parser: 'typescript',
singleQuote: true
};

return prettier.format(source, options);
}

0 comments on commit 4973d93

Please sign in to comment.