Skip to content

Commit

Permalink
Convert build-package to Typescript
Browse files Browse the repository at this point in the history
  • Loading branch information
valentinpalkovic committed Nov 3, 2023
1 parent 6a619a3 commit 8286a8d
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 31 deletions.
2 changes: 1 addition & 1 deletion code/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
},
"scripts": {
"await-serve-storybooks": "wait-on http://localhost:8001",
"build": "NODE_ENV=production node ../scripts/build-package.js",
"build": "NODE_ENV=production ts-node --swc --esm ../scripts/build-package.ts",
"changelog": "pr-log --sloppy --cherry-pick",
"changelog:next": "pr-log --sloppy --since-prerelease",
"check": "NODE_ENV=production node ../scripts/check-package.js",
Expand Down
64 changes: 34 additions & 30 deletions scripts/build-package.js → scripts/build-package.ts
Original file line number Diff line number Diff line change
@@ -1,39 +1,41 @@
#!/usr/bin/env node

/* eslint-disable global-require */
const { resolve, join, posix, sep } = require('path');
const { readJSON } = require('fs-extra');

async function getWorkspaces(includePrivate = true) {
const { execaCommand } = await import('execa');
const { stdout } = await execaCommand(`yarn workspaces list --json --no-private`, {
cwd: join(__dirname, '..', 'code'),
shell: true,
});
return JSON.parse(`[${stdout.split('\n').join(',')}]`);
}
import { resolve, posix, sep } from 'path';
import { readJSON } from 'fs-extra';
import prompts from 'prompts';
import program from 'commander';
import chalk from 'chalk';
import { getWorkspaces } from './utils/workspace';
import { execaCommand } from './utils/exec';

async function run() {
const prompts = require('prompts');
const program = require('commander');
const chalk = require('chalk');

const packages = await getWorkspaces();
const packageTasks = packages
.map((package) => {
.map((pkg) => {
return {
...package,
suffix: package.name.replace('@storybook/', ''),
...pkg,
suffix: pkg.name.replace('@storybook/', ''),
defaultValue: false,
helpText: `build only the ${package.name} package`,
helpText: `build only the ${pkg.name} package`,
};
})
.reduce((acc, next) => {
acc[next.name] = next;
return acc;
}, {});
}, {} as Record<string, { name: string; defaultValue: boolean; suffix: string; helpText: string }>);

const tasks = {
const tasks: Record<
string,
{
name: string;
defaultValue: boolean;
suffix: string;
helpText: string;
value?: any;
location?: string;
}
> = {
watch: {
name: `watch`,
defaultValue: false,
Expand Down Expand Up @@ -92,14 +94,15 @@ async function run() {
name: 'todo',
min: 1,
hint: 'You can also run directly with package name like `yarn build core`, or `yarn build --all` for all packages!',
// @ts-expect-error @types incomplete
optionsPerPage: require('window-size').height - 3, // 3 lines for extra info
choices: packages.map(({ name: key }) => ({
value: key,
title: tasks[key].name || key,
selected: (tasks[key] && tasks[key].defaultValue) || false,
})),
},
]).then(({ watch, prod, todo }) => {
]).then(({ watch, prod, todo }: { watch: boolean; prod: boolean; todo: Array<string> }) => {
watchMode = watch;
prodMode = prod;
return todo?.map((key) => tasks[key]);
Expand All @@ -119,9 +122,8 @@ async function run() {
.join(sep);

const cwd = resolve(__dirname, '..', 'code', v.location);
const { execaCommand } = await import('execa');
const tsNode = require.resolve('ts-node/dist/bin');
const sub = execaCommand(
const sub = await execaCommand(
`node ${tsNode} ${commmand}${watchMode ? ' --watch' : ''}${prodMode ? ' --optimized' : ''}`,
{
cwd,
Expand All @@ -134,16 +136,18 @@ async function run() {
}
);

sub.stdout.on('data', (data) => {
process.stdout.write(`${chalk.cyan(v.name)}:\n${data}`);
});
sub.stderr.on('data', (data) => {
process.stderr.write(`${chalk.red(v.name)}:\n${data}`);
});
if (sub.stdout) {
process.stdout.write(`${chalk.cyan(v.name)}:\n${sub.stdout}`);
}

if (sub.stderr) {
process.stderr.write(`${chalk.red(v.name)}:\n${sub.stderr}`);
}
});
}

run().catch((e) => {
// eslint-disable-next-line no-console
console.log(e);
process.exit(1);
});

0 comments on commit 8286a8d

Please sign in to comment.