Skip to content

Commit

Permalink
build: run prettier on generated files (#3018)
Browse files Browse the repository at this point in the history
  • Loading branch information
IvanGoncharov committed Apr 4, 2021
1 parent 10bd6fe commit 142ca1f
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 8 deletions.
12 changes: 11 additions & 1 deletion resources/build-deno.js
Expand Up @@ -4,9 +4,14 @@ const fs = require('fs');
const path = require('path');

const babel = require('@babel/core');
const prettier = require('prettier');

const { readdirRecursive, showDirStats } = require('./utils');

const prettierConfig = JSON.parse(
fs.readFileSync(require.resolve('../.prettierrc'), 'utf-8'),
);

if (require.main === module) {
fs.rmdirSync('./denoDist', { recursive: true, force: true });
fs.mkdirSync('./denoDist');
Expand All @@ -20,7 +25,7 @@ if (require.main === module) {
if (filepath.endsWith('.js')) {
const options = { babelrc: false, configFile: './.babelrc-deno.json' };
const output = babel.transformFileSync(srcPath, options).code + '\n';
fs.writeFileSync(destPath, output);
writeGeneratedFile(destPath, output);
} else if (filepath.endsWith('.d.ts')) {
fs.copyFileSync(srcPath, destPath);
}
Expand All @@ -31,3 +36,8 @@ if (require.main === module) {

showDirStats('./denoDist');
}

function writeGeneratedFile(filepath, body) {
const formatted = prettier.format(body, { filepath, ...prettierConfig });
fs.writeFileSync(filepath, formatted);
}
22 changes: 15 additions & 7 deletions resources/build-npm.js
Expand Up @@ -5,9 +5,14 @@ const path = require('path');
const assert = require('assert');

const babel = require('@babel/core');
const prettier = require('prettier');

const { readdirRecursive, showDirStats } = require('./utils');

const prettierConfig = JSON.parse(
fs.readFileSync(require.resolve('../.prettierrc'), 'utf-8'),
);

if (require.main === module) {
fs.rmSync('./npmDist', { recursive: true, force: true });
fs.mkdirSync('./npmDist');
Expand All @@ -19,14 +24,15 @@ if (require.main === module) {

fs.mkdirSync(path.dirname(destPath), { recursive: true });
if (filepath.endsWith('.js')) {
const flowBody = '// @flow strict\n' + fs.readFileSync(srcPath, 'utf-8');
const flowBody =
'// @flow strict\n\n' + fs.readFileSync(srcPath, 'utf-8');
fs.writeFileSync(destPath + '.flow', flowBody);

const cjs = babelBuild(srcPath, { envName: 'cjs' });
fs.writeFileSync(destPath, cjs);
writeGeneratedFile(destPath, cjs);

const mjs = babelBuild(srcPath, { envName: 'mjs' });
fs.writeFileSync(destPath.replace(/\.js$/, '.mjs'), mjs);
writeGeneratedFile(destPath.replace(/\.js$/, '.mjs'), mjs);
} else if (filepath.endsWith('.d.ts')) {
fs.copyFileSync(srcPath, destPath);
}
Expand All @@ -37,14 +43,16 @@ if (require.main === module) {

// Should be done as the last step so only valid packages can be published
const packageJSON = buildPackageJSON();
fs.writeFileSync(
'./npmDist/package.json',
JSON.stringify(packageJSON, null, 2),
);
writeGeneratedFile('./npmDist/package.json', JSON.stringify(packageJSON));

showDirStats('./npmDist');
}

function writeGeneratedFile(filepath, body) {
const formatted = prettier.format(body, { filepath, ...prettierConfig });
fs.writeFileSync(filepath, formatted);
}

function babelBuild(srcPath, options) {
const { code } = babel.transformFileSync(srcPath, {
babelrc: false,
Expand Down

0 comments on commit 142ca1f

Please sign in to comment.