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
+13 -13
View File
@@ -1,6 +1,9 @@
const fse = require('fs-extra');
const path = require('path');
const generateInfo = require('./generate-info');
import esMain from 'es-main';
import fse from 'fs-extra';
import generateInfo from './generate-info.js';
import path from 'path';
import {dirname} from 'path';
import {fileURLToPath} from 'url';
/**
* Read the symbols from info file.
@@ -21,11 +24,11 @@ function getImport(symbol, member) {
const defaultExport = symbol.name.split('~');
const namedExport = symbol.name.split('.');
if (defaultExport.length > 1 && defaultExport[0].indexOf('.') === -1) {
const from = defaultExport[0].replace(/^module\:/, './');
const from = defaultExport[0].replace(/^module\:/, './') + '.js';
const importName = from.replace(/[.\/]+/g, '$');
return `import ${importName} from '${from}';`;
} else if (namedExport.length > 1 && member) {
const from = namedExport[0].replace(/^module\:/, './');
const from = namedExport[0].replace(/^module\:/, './') + '.js';
const importName = from.replace(/[.\/]+/g, '_');
return `import {${member} as ${importName}$${member}} from '${from}';`;
}
@@ -97,7 +100,7 @@ function generateExports(symbols) {
* Generate the exports code.
* @return {Promise<string>} Resolves with the exports code.
*/
async function main() {
export default async function main() {
const symbols = await getSymbols();
return generateExports(symbols);
}
@@ -106,18 +109,15 @@ async function main() {
* If running this module directly, read the config file, call the main
* function, and write the output file.
*/
if (require.main === module) {
if (esMain(import.meta)) {
const baseDir = dirname(fileURLToPath(import.meta.url));
main()
.then(async (code) => {
const filepath = path.join(__dirname, '..', 'build', 'index.js');
const filepath = path.join(baseDir, '..', 'build', 'index.js');
await fse.outputFile(filepath, code);
})
.catch((err) => {
process.stderr.write(`${err.message}\n`, () => process.exit(1));
});
}
/**
* Export main function.
*/
module.exports = main;
+24 -18
View File
@@ -1,11 +1,16 @@
const fse = require('fs-extra');
const path = require('path');
const spawn = require('child_process').spawn;
const walk = require('walk').walk;
const isWindows = process.platform.indexOf('win') === 0;
import esMain from 'es-main';
import fse from 'fs-extra';
import path from 'path';
import {dirname} from 'path';
import {fileURLToPath} from 'url';
import {spawn} from 'child_process';
import {walk} from 'walk';
const sourceDir = path.join(__dirname, '..', 'src');
const infoPath = path.join(__dirname, '..', 'build', 'info.json');
const isWindows = process.platform.indexOf('win') === 0;
const baseDir = dirname(fileURLToPath(import.meta.url));
const sourceDir = path.join(baseDir, '..', 'src');
const infoPath = path.join(baseDir, '..', 'build', 'info.json');
/**
* Get checked path of a binary.
@@ -17,9 +22,15 @@ function getBinaryPath(binaryName) {
binaryName += '.cmd';
}
const jsdocResolved = require.resolve('jsdoc/jsdoc.js');
const jsdocResolved = path.join(
baseDir,
'..',
'node_modules',
'jsdoc',
'jsdoc.js'
);
const expectedPaths = [
path.join(__dirname, '..', 'node_modules', '.bin', binaryName),
path.join(baseDir, '..', 'node_modules', '.bin', binaryName),
path.resolve(
path.join(path.dirname(jsdocResolved), '..', '.bin', binaryName)
),
@@ -40,7 +51,7 @@ function getBinaryPath(binaryName) {
const jsdoc = getBinaryPath('jsdoc');
const jsdocConfig = path.join(
__dirname,
baseDir,
'..',
'config',
'jsdoc',
@@ -120,7 +131,7 @@ function spawnJSDoc(paths) {
return new Promise((resolve, reject) => {
let output = '';
let errors = '';
const cwd = path.join(__dirname, '..');
const cwd = path.join(baseDir, '..');
const child = spawn(jsdoc, ['-c', jsdocConfig].concat(paths), {cwd: cwd});
child.stdout.on('data', (data) => {
@@ -162,7 +173,7 @@ async function write(info) {
* Generate info from the sources.
* @return {Promise<Error>} Resolves with the info object.
*/
async function main() {
export default async function main() {
const paths = await getPaths();
return await spawnJSDoc(paths);
}
@@ -170,15 +181,10 @@ async function main() {
/**
* If running this module directly, generate and write out the info.json file.
*/
if (require.main === module) {
if (esMain(import.meta)) {
main()
.then(write)
.catch((err) => {
process.stderr.write(`${err.message}\n`, () => process.exit(1));
});
}
/**
* Export main function.
*/
module.exports = main;
+16 -5
View File
@@ -1,7 +1,11 @@
const version = require('../package.json').version;
const semver = require('semver');
import esMain from 'es-main';
import process from 'process';
import semver from 'semver';
import {promises as fs} from 'fs';
function nextVersion() {
async function nextVersion() {
const pkg = await fs.readFile('../package.json', {encoding: 'utf8'});
const version = JSON.parse(pkg).version;
const s = semver.parse(version);
if (!s) {
throw new Error(`Invalid version ${version}`);
@@ -9,6 +13,13 @@ function nextVersion() {
return `${s.major}.${s.minor}.${s.patch}-dev.${Date.now()}`;
}
if (require.main === module) {
process.stdout.write(`${nextVersion()}\n`);
if (esMain(import.meta)) {
nextVersion()
.then((version) => {
process.stdout.write(`${version}\n`);
})
.catch((error) => {
process.stderr.write(`${error}\n`);
process.exit(1);
});
}
+45 -31
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));
});
}
@@ -1,3 +1,5 @@
/* eslint-disable import/no-commonjs */
const path = require('path');
const babel = require('@rollup/plugin-babel').babel;
const resolve = require('@rollup/plugin-node-resolve').nodeResolve;