Skip to content

Commit

Permalink
feat(Build): add canary argument for build script
Browse files Browse the repository at this point in the history
  • Loading branch information
hamikhambardzumyan committed Feb 20, 2024
1 parent 17718b4 commit bc6cec8
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 9 deletions.
8 changes: 5 additions & 3 deletions scripts/addVersionHistory.js
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
20 changes: 19 additions & 1 deletion scripts/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -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', {
Expand All @@ -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!'
Expand All @@ -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,
Expand Down
36 changes: 31 additions & 5 deletions scripts/utils.js
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -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
};

0 comments on commit bc6cec8

Please sign in to comment.