Skip to content

build: run prettier on generated files #3018

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 4, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion resources/build-deno.js
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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