Skip to content

Commit

Permalink
fix(scripts): better handling of the nx formatter - add prettierignore (
Browse files Browse the repository at this point in the history
  • Loading branch information
m-abs authored and NathanWalker committed Jan 19, 2019
1 parent 26415c6 commit c8c32da
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 24 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"watch": "tsc -w -p tsconfig.json",
"test": "npm run build && jasmine src/**/*_spec.js",
"prepare": "npm run build",
"postinstall": "node scripts/postinstall.js",
"postinstall": "[ ! -f scripts/postinstall.js ] || node scripts/postinstall.js",
"debug": "node --debug-brk ./node_modules/@angular/cli/bin/ng g command",
"changelog": "conventional-changelog -p angular -i CHANGELOG.md -s"
},
Expand Down
89 changes: 66 additions & 23 deletions scripts/postinstall.ts
Original file line number Diff line number Diff line change
@@ -1,41 +1,55 @@
import * as fs from 'fs';
import * as path from 'path';
import { promisify } from 'util';

export function updateConfig() {
const fsExists = promisify(fs.exists);
const fsWriteFile = promisify(fs.writeFile);
const fsReadFile = promisify(fs.readFile);

export async function updateConfig() {
const cwd = process.cwd();
if (cwd.indexOf('xplat/schematics') > -1) {
if (cwd.indexOf('node_modules/@nstudio/schematics') === -1) {
// ignore: local development
} else {
const ngCliConfigPath = path.join(process.cwd(), '/../../..', 'angular.json');
// console.log(ngCliConfigPath);
try {
const config = fs.readFileSync(ngCliConfigPath, 'UTF-8');
if (config) {
const ngCli = JSON.parse(config);
// update default
ngCli.cli.defaultCollection = "@nstudio/schematics";
fs.writeFileSync(ngCliConfigPath, JSON.stringify(ngCli, null, 2));
}
} catch (err) {
console.warn('An issue was detected during installation: angular.json does not exist.');
return;
}

const ngCliConfigPath = path.join(process.cwd(), '/../../..', 'angular.json');
// console.log(ngCliConfigPath);
try {
const config = fs.readFileSync(ngCliConfigPath, 'UTF-8');
if (config) {
const ngCli = JSON.parse(config);
// update default
ngCli.cli.defaultCollection = "@nstudio/schematics";
fs.writeFileSync(ngCliConfigPath, JSON.stringify(ngCli, null, 2));
}
} catch (err) {
console.warn('An issue was detected during installation: angular.json does not exist.');
}

try {
// Prevent Nrwl formatter from walking into {N} platforms folder
fixFormatter();
await fixFormatter();
} catch (err) {
console.log('An issue were detected during patching the nx-formatter', err);
console.error('An issue were detected during patching the nx-formatter', err);
}
}

updateConfig();
try {
await makePrettierIgnore()
} catch (err) {
console.error('An issue were detected during patching the nx-formatter', err);
}
}

export function fixFormatter() {
/**
* @nrwl/nx's formatter doesn't include files in the xplat-folder.
* This function patches their formatter cli to include the xplat-folder
*/
export async function fixFormatter() {
const formatPath = path.join(process.cwd(), '/../..', '@nrwl/schematics/src/command-line/format.js');
let formatContent = fs.readFileSync(formatPath, 'UTF-8');
let formatContent = await fsReadFile(formatPath, 'UTF-8');

const patchLine = ` // PATCHED by @nstudio/schematics\n patterns.push('"xplat/**/*.ts"', '"!apps/**/platforms/{android,ios}/**/*"');`;
const patchLine = ` // PATCHED by @nstudio/schematics\n patterns.push('"xplat/**/*"');`;
if (formatContent.indexOf(patchLine) !== -1) {
console.log(`Patch for nx format have already been applied`);
return;
Expand All @@ -48,10 +62,39 @@ export function fixFormatter() {

const newFormatContent = formatContent.replace(patchRegExp, `${patchLine}\n$1`);
if (formatContent !== newFormatContent) {
fs.writeFileSync(formatPath, newFormatContent);
await fsWriteFile(formatPath, newFormatContent);
console.log('Patch for nx format have been applied');
} else {
throw new Error(`Apply couldn't patch for nx format`);
}
}

/**
* To avoid @nrwl/nx's formatter tries to format App_Resources, platforms-files etc.
* Create a .prettierignore file at the root of the project.
*/
export async function makePrettierIgnore() {
const prettierIgnorePath = path.join(process.cwd(), '/../..', '.prettierignore');

const prettierIgnore = `**/*.d.ts
apps/**/platforms/{android,ios}/**/*
**/App_Resources/**/*
apps/nativescript-*/tools/**/*
**/webpack.config.js
**/package.json
**/package-lock.json
**/tslint.json
**/tsconfig.*.json
**/tsconfig.json
**/*.conf.js
`;

if (!await fsExists(prettierIgnorePath)) {
console.log(`"${prettierIgnorePath}" already exists`);
return;
}

await fsWriteFile(prettierIgnorePath, prettierIgnore, 'UTF-8');
}

updateConfig();

0 comments on commit c8c32da

Please sign in to comment.