diff --git a/scripts/addVersionHistory.js b/scripts/addVersionHistory.js index 26d0373a..6eaa0d76 100644 --- a/scripts/addVersionHistory.js +++ b/scripts/addVersionHistory.js @@ -10,13 +10,15 @@ const addVersionToHistory = async () => { try { const libVersionsFromRepo = await execCommand('npm view @geneui/components versions'); - const libVersionsFromRepoArr = libVersionsFromRepo + const libStabileVersionsFromRepo = libVersionsFromRepo .split(',') - .map((version) => `v${version.replace(/[\[\]\\n']/g, '').trim()}`); + .map((version) => `${version.replace(/[\[\]\\n']/g, '').trim()}`) + .filter((version) => version.match(/^\d+\.\d+\.\d+$/)) + .map((version) => `v${version}`); // .filter((version) => version.endsWith('0')); const libVersions = { - versions: libVersionsFromRepoArr + versions: libStabileVersionsFromRepo }; await fs.writeFile('./.storybook/lib-versions.json', JSON.stringify(libVersions)); diff --git a/scripts/build.js b/scripts/build.js index e40a1414..ea6e6e83 100644 --- a/scripts/build.js +++ b/scripts/build.js @@ -5,7 +5,7 @@ import yargs from 'yargs'; import { rmSync } from 'fs'; import { resolve } from 'path'; -import { execCommand, copyStaticFilesToDist } from './utils'; +import { execCommand, copyStaticFilesToDist, replaceCanaryVersionInDistPGK } from './utils'; // In case of true the build script should skip lint and semver steps const { pure: isBuildRunInPureMode } = yargs.option('pure', { @@ -14,6 +14,20 @@ const { pure: isBuildRunInPureMode } = yargs.option('pure', { default: false }).argv; +// The canary version branch name. If provided package json file version will be replaced +const { canary: canaryVersion } = yargs.option('canary', { + describe: 'If --canary argument is specified then package json version will be generated as canary version.', + type: 'string', + default: null +}).argv; + +// The commit SHA. It should be provided for canary versions only +const { commitSHA } = yargs.option('commitSHA', { + describe: 'If --canary version is provided this argument should be provided too --commitSHA.', + type: 'string', + default: null +}).argv; + const spinner = ora({ color: 'yellow', fail: 'Something went wrong please see errors bellow!' @@ -30,6 +44,10 @@ const build = async () => { await execCommand('rollup -c ./configs/rollup.config.js --bundleConfigAsCjs', 'rollup.config'); await copyStaticFilesToDist(); + + if (canaryVersion && commitSHA) { + await replaceCanaryVersionInDistPGK(canaryVersion, commitSHA); + } } catch (error) { return { hasError: true, diff --git a/scripts/utils.js b/scripts/utils.js index ce2f9d3c..bce724a6 100644 --- a/scripts/utils.js +++ b/scripts/utils.js @@ -1,8 +1,9 @@ import chalk from 'chalk'; import { lstatSync, readdirSync } from 'fs'; -import { copyFile, stat } from 'fs/promises'; -import { join } from 'path'; +import { copyFile, stat, readFile, writeFile } from 'fs/promises'; +import { join, resolve } from 'path'; import { exec } from 'child_process'; +import dayjs from 'dayjs'; /** * Executes a shell command and return it as a Promise. @@ -56,10 +57,35 @@ const isFileExists = async (filePath) => { } catch (error) { if (error.code === 'ENOENT') { return false; - } else { + } throw error; - } + } }; -export { execCommand, isDirectory, isFile, isFileExists, getDirectories, getFiles, copyStaticFilesToDist }; +const replaceCanaryVersionInDistPGK = async (canaryVersion, commitSHA) => { + try { + const [version] = canaryVersion.split('/').reverse(); + const packageJsonFile = await readFile(resolve(__dirname, '../dist/package.json'), 'utf8'); + const packageJson = JSON.parse(packageJsonFile); + packageJson.version = `${version}-canary-${commitSHA}-${dayjs().format('DDMMYYYY')}`; + await writeFile(resolve(__dirname, '../dist/package.json'), JSON.stringify(packageJson, null, 4), 'utf8'); + } catch (error) { + if (error.code === 'ENOENT') { + return false; + } + throw error; + + } +}; + +export { + execCommand, + isDirectory, + isFile, + isFileExists, + getDirectories, + getFiles, + copyStaticFilesToDist, + replaceCanaryVersionInDistPGK +};