Skip to content

Commit

Permalink
build: run prettier on generated files
Browse files Browse the repository at this point in the history
  • Loading branch information
IvanGoncharov committed Apr 3, 2021
1 parent 10bd6fe commit e24624e
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 7 deletions.
13 changes: 12 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,9 @@ if (require.main === module) {

showDirStats('./denoDist');
}

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

19 changes: 13 additions & 6 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 @@ -23,10 +28,10 @@ if (require.main === module) {
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 +42,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 e24624e

Please sign in to comment.