2022-10-05 12:57:24 -04:00
|
|
|
import loconfig from '../utils/config.js';
|
|
|
|
|
import message from '../utils/message.js';
|
|
|
|
|
import resolve from '../utils/template.js';
|
2022-10-06 11:55:46 -04:00
|
|
|
import { randomBytes } from 'node:crypto';
|
2022-10-05 12:57:24 -04:00
|
|
|
import {
|
|
|
|
|
mkdir,
|
|
|
|
|
readFile,
|
|
|
|
|
writeFile,
|
|
|
|
|
} from 'node:fs/promises';
|
|
|
|
|
import {
|
|
|
|
|
basename,
|
|
|
|
|
dirname,
|
|
|
|
|
} from 'node:path';
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @typedef {object} VersionOptions
|
2022-10-06 11:55:46 -04:00
|
|
|
* @property {string|number|null} prettyPrint - A string or number to insert
|
|
|
|
|
* white space into the output JSON string for readability purposes.
|
|
|
|
|
* @property {string} versionFormat - The version number format.
|
|
|
|
|
* @property {string} versionKey - The JSON field name assign
|
|
|
|
|
* the version number to.
|
2022-10-05 12:57:24 -04:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @const {VersionOptions} defaultVersionOptions - The default shared version options.
|
|
|
|
|
* @const {VersionOptions} developmentVersionOptions - The predefined version options for development.
|
|
|
|
|
* @const {VersionOptions} productionVersionOptions - The predefined version options for production.
|
|
|
|
|
*/
|
|
|
|
|
export const defaultVersionOptions = {
|
2022-10-06 11:55:46 -04:00
|
|
|
prettyPrint: 4,
|
|
|
|
|
versionFormat: 'timestamp',
|
|
|
|
|
versionKey: 'version',
|
2022-10-05 12:57:24 -04:00
|
|
|
};
|
|
|
|
|
export const developmentVersionOptions = Object.assign({}, defaultVersionOptions);
|
|
|
|
|
export const productionVersionOptions = Object.assign({}, defaultVersionOptions);
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @const {object} developmentVersionFilesArgs - The predefined `bumpVersion()` options for development.
|
|
|
|
|
* @const {object} productionVersionFilesArgs - The predefined `bumpVersion()` options for production.
|
|
|
|
|
*/
|
|
|
|
|
export const developmentVersionFilesArgs = [
|
|
|
|
|
developmentVersionOptions,
|
|
|
|
|
];
|
|
|
|
|
export const productionVersionFilesArgs = [
|
|
|
|
|
productionVersionOptions,
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Bumps version numbers in file.
|
|
|
|
|
*
|
|
|
|
|
* @async
|
|
|
|
|
* @param {object} [versionOptions=null] - Customize the version options.
|
|
|
|
|
* If `null`, default production options are used.
|
|
|
|
|
* @return {Promise}
|
|
|
|
|
*/
|
|
|
|
|
export default async function bumpVersion(versionOptions = null) {
|
|
|
|
|
if (versionOptions == null) {
|
|
|
|
|
versionOptions = productionVersionOptions;
|
|
|
|
|
} else if (
|
|
|
|
|
versionOptions !== developmentVersionOptions &&
|
|
|
|
|
versionOptions !== productionVersionOptions
|
|
|
|
|
) {
|
|
|
|
|
versionOptions = Object.assign({}, defaultVersionOptions, versionOptions);
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-06 11:55:46 -04:00
|
|
|
const queue = new Map();
|
|
|
|
|
|
|
|
|
|
loconfig.tasks.versions.forEach(({
|
2022-10-05 12:57:24 -04:00
|
|
|
outfile,
|
2022-10-06 11:55:46 -04:00
|
|
|
label = null,
|
|
|
|
|
...options
|
2022-10-05 12:57:24 -04:00
|
|
|
}) => {
|
|
|
|
|
if (!label) {
|
|
|
|
|
label = basename(outfile || 'undefined');
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-06 11:55:46 -04:00
|
|
|
options.pretty = (options.pretty ?? versionOptions.prettyPrint);
|
|
|
|
|
options.format = (options.format ?? versionOptions.versionFormat);
|
|
|
|
|
options.key = (options.key ?? versionOptions.versionKey);
|
2022-10-05 12:57:24 -04:00
|
|
|
|
2022-10-06 11:55:46 -04:00
|
|
|
if (queue.has(outfile)) {
|
|
|
|
|
queue.get(outfile).then(() => handleBumpVersion(outfile, label, options));
|
|
|
|
|
} else {
|
|
|
|
|
queue.set(outfile, handleBumpVersion(outfile, label, options));
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
};
|
2022-10-05 12:57:24 -04:00
|
|
|
|
2022-10-06 11:55:46 -04:00
|
|
|
/**
|
|
|
|
|
* Creates a formatted version number or string.
|
|
|
|
|
*
|
|
|
|
|
* @param {string} format - The version format.
|
|
|
|
|
* @return {string|number}
|
|
|
|
|
*/
|
|
|
|
|
function createVersionNumber(format) {
|
|
|
|
|
let [ type, modifier ] = format.split(':');
|
2022-10-05 12:57:24 -04:00
|
|
|
|
2022-10-06 11:55:46 -04:00
|
|
|
switch (type) {
|
|
|
|
|
case 'hex':
|
|
|
|
|
case 'hexadecimal':
|
|
|
|
|
modifier = Number.parseInt(modifier);
|
2022-10-05 12:57:24 -04:00
|
|
|
|
2022-10-06 11:55:46 -04:00
|
|
|
if (Number.isNaN(modifier)) {
|
|
|
|
|
modifier = 6;
|
2022-10-05 12:57:24 -04:00
|
|
|
}
|
|
|
|
|
|
2022-10-06 11:55:46 -04:00
|
|
|
return randomBytes(modifier).toString('hex');
|
|
|
|
|
|
|
|
|
|
case 'timestamp':
|
|
|
|
|
return Date.now().valueOf();
|
|
|
|
|
}
|
2022-10-05 12:57:24 -04:00
|
|
|
|
2022-10-06 11:55:46 -04:00
|
|
|
throw new TypeError(
|
|
|
|
|
'Expected \'format\' to be either "timestamp" or "hexadecimal"'
|
|
|
|
|
);
|
|
|
|
|
}
|
2022-10-05 12:57:24 -04:00
|
|
|
|
2022-10-06 11:55:46 -04:00
|
|
|
/**
|
|
|
|
|
* @async
|
|
|
|
|
* @param {string} outfile
|
|
|
|
|
* @param {string} label
|
|
|
|
|
* @param {object} options
|
|
|
|
|
* @return {Promise}
|
|
|
|
|
*/
|
|
|
|
|
async function handleBumpVersion(outfile, label, options) {
|
|
|
|
|
const timeLabel = `${label} bumped in`;
|
|
|
|
|
console.time(timeLabel);
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
outfile = resolve(outfile);
|
|
|
|
|
|
|
|
|
|
let json;
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
json = JSON.parse(await readFile(outfile));
|
2022-10-05 12:57:24 -04:00
|
|
|
} catch (err) {
|
2022-10-06 11:55:46 -04:00
|
|
|
json = {};
|
|
|
|
|
|
|
|
|
|
message(`${label} is a new file`, 'notice');
|
2022-10-05 12:57:24 -04:00
|
|
|
|
2022-10-06 11:55:46 -04:00
|
|
|
await mkdir(dirname(outfile), { recursive: true });
|
2022-10-05 12:57:24 -04:00
|
|
|
}
|
2022-10-06 11:55:46 -04:00
|
|
|
|
|
|
|
|
json[options.key] = createVersionNumber(options.format);
|
|
|
|
|
|
|
|
|
|
await writeFile(outfile, JSON.stringify(json, null, options.pretty));
|
|
|
|
|
|
|
|
|
|
message(`${label} bumped`, 'success', timeLabel);
|
|
|
|
|
} catch (err) {
|
|
|
|
|
message(`Error bumping ${label}`, 'error');
|
|
|
|
|
message(err);
|
|
|
|
|
|
|
|
|
|
notification({
|
|
|
|
|
title: `${label} bumping failed 🚨`,
|
|
|
|
|
message: `${err.name}: ${err.message}`
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|