Modules all the way

This commit is contained in:
Tim Schaub
2020-02-20 18:30:09 -07:00
parent ae70a1fb9d
commit c301d2413b
29 changed files with 316 additions and 173 deletions

View File

@@ -1,35 +1,49 @@
const fs = require('fs');
const path = require('path');
const pkg = require('../package.json');
import esMain from 'es-main';
import fse from 'fs-extra';
import path from 'path';
import {dirname} from 'path';
import {fileURLToPath} from 'url';
const buildDir = path.resolve(__dirname, '../build/ol');
const baseDir = dirname(fileURLToPath(import.meta.url));
const buildDir = path.resolve(baseDir, '../build/ol');
// update the version number in util.js
const utilPath = path.join(buildDir, 'util.js');
const versionRegEx = /var VERSION = '(.*)';/g;
const utilSrc = fs
.readFileSync(utilPath, 'utf-8')
.replace(versionRegEx, `var VERSION = '${pkg.version}';`);
fs.writeFileSync(utilPath, utilSrc, 'utf-8');
async function main() {
const pkg = await fse.readJSON(path.resolve(baseDir, '../package.json'));
// write out simplified package.json
delete pkg.scripts;
delete pkg.devDependencies;
delete pkg.style;
delete pkg.eslintConfig;
delete pkg.private;
fs.writeFileSync(
path.join(buildDir, 'package.json'),
JSON.stringify(pkg, null, 2),
'utf-8'
);
// update the version number in util.js
const utilPath = path.join(buildDir, 'util.js');
const versionRegEx = /var VERSION = '(.*)';/g;
let utilSrc = await fse.readFile(utilPath, 'utf-8');
utilSrc = utilSrc.replace(versionRegEx, `var VERSION = '${pkg.version}';`);
await fse.writeFile(utilPath, utilSrc, 'utf-8');
// copy in readme and license files
fs.copyFileSync(
path.resolve(__dirname, '../README.md'),
path.join(buildDir, 'README.md')
);
fs.copyFileSync(
path.resolve(__dirname, '../LICENSE.md'),
path.join(buildDir, 'LICENSE.md')
);
// write out simplified package.json
pkg.main = 'index.js';
delete pkg.scripts;
delete pkg.devDependencies;
delete pkg.style;
delete pkg.eslintConfig;
delete pkg.private;
await fse.writeJSON(path.join(buildDir, 'package.json'), pkg, {spaces: 2});
// copy in readme and license files
await fse.copyFile(
path.resolve(baseDir, '../README.md'),
path.join(buildDir, 'README.md')
);
await fse.copyFile(
path.resolve(baseDir, '../LICENSE.md'),
path.join(buildDir, 'LICENSE.md')
);
}
/**
* If running this module directly, read the config file, call the main
* function, and write the output file.
*/
if (esMain(import.meta)) {
main().catch((err) => {
process.stderr.write(`${err.message}\n`, () => process.exit(1));
});
}