From 55f1d54e94cc59e21ad41799e742ce730fd7aa83 Mon Sep 17 00:00:00 2001 From: Sebastien LE MOUILLOUR <32456736+smouillour@users.noreply.github.com> Date: Tue, 5 Dec 2023 15:03:05 +0100 Subject: [PATCH] fix(script-core): make a solution compatible with windows and pnpm (#5037) --- .changeset/lazy-badgers-shave.md | 5 +++++ tools/scripts-core/src/scripts/build-lib-umd.js | 9 +++++++-- tools/scripts-core/src/scripts/build-lib.js | 9 ++++++--- .../scripts-core/src/scripts/build-storybook.js | 5 +++-- tools/scripts-core/src/scripts/build.js | 4 +++- .../scripts-core/src/scripts/start-storybook.js | 5 +++-- tools/scripts-core/src/scripts/start.js | 4 +++- tools/scripts-core/src/scripts/test.js | 9 +++++---- tools/scripts-core/src/utils/bin.js | 16 ++++++++++++++++ 9 files changed, 51 insertions(+), 15 deletions(-) create mode 100644 .changeset/lazy-badgers-shave.md create mode 100644 tools/scripts-core/src/utils/bin.js diff --git a/.changeset/lazy-badgers-shave.md b/.changeset/lazy-badgers-shave.md new file mode 100644 index 0000000000..42230aea54 --- /dev/null +++ b/.changeset/lazy-badgers-shave.md @@ -0,0 +1,5 @@ +--- +'@talend/scripts-core': patch +--- + +fix(script-core): make a solution compatible with windows and pnpm diff --git a/tools/scripts-core/src/scripts/build-lib-umd.js b/tools/scripts-core/src/scripts/build-lib-umd.js index 3bcb25cf40..47a4a05e7e 100644 --- a/tools/scripts-core/src/scripts/build-lib-umd.js +++ b/tools/scripts-core/src/scripts/build-lib-umd.js @@ -2,14 +2,19 @@ import fs from 'fs'; import * as utils from '@talend/scripts-utils'; +import { resolveScript } from '../utils/bin.js'; import { getUserConfigFile } from '../utils/env.js'; const dirname = utils.path.getDirName(import.meta.url); async function buildUMD(env, presetApi, options = []) { return utils.process.spawn( - new URL(import.meta.resolve('webpack/bin/webpack.js')).pathname, - ['--config', utils.path.hereRelative(dirname, '../config/webpack.config.js')].concat(options), + 'node', + [ + resolveScript('webpack/bin/webpack.js'), + '--config', + utils.path.hereRelative(dirname, '../config/webpack.config.js'), + ].concat(options), { stdio: 'inherit', env }, ); } diff --git a/tools/scripts-core/src/scripts/build-lib.js b/tools/scripts-core/src/scripts/build-lib.js index 8652544331..65ba1875c8 100755 --- a/tools/scripts-core/src/scripts/build-lib.js +++ b/tools/scripts-core/src/scripts/build-lib.js @@ -7,6 +7,7 @@ import { fileURLToPath } from 'url'; import * as utils from '@talend/scripts-utils'; +import { resolveScript } from '../utils/bin.js'; import { getUserConfigFile } from '../utils/env.js'; const pkgPath = path.join(process.cwd(), 'package.json'); @@ -53,8 +54,9 @@ export default async function build(env, presetApi, unsafeOptions) { console.log('Compiling with babel...'); utils.process .spawn( - utils.path.resolveBin('babel'), + 'node', [ + resolveScript('@babel/cli/bin/babel.js'), '--config-file', babelConfigPath, '-d', @@ -104,10 +106,11 @@ export default async function build(env, presetApi, unsafeOptions) { } else { console.log('Building with tsc'); } + const tsc = resolveScript('typescript/bin/tsc'); + args = [tsc].concat(args); - const tsc = utils.path.resolveBin('tsc'); utils.process - .spawn(tsc, args, { stdio: 'inherit', env }) + .spawn('node', args, { stdio: 'inherit', env }) .then(tscSpawn => { tscSpawn.on('exit', status => { if (parseInt(status, 10) !== 0) { diff --git a/tools/scripts-core/src/scripts/build-storybook.js b/tools/scripts-core/src/scripts/build-storybook.js index dafa4835fd..05ab5902be 100755 --- a/tools/scripts-core/src/scripts/build-storybook.js +++ b/tools/scripts-core/src/scripts/build-storybook.js @@ -1,13 +1,14 @@ import * as utils from '@talend/scripts-utils'; +import { resolveScript } from '../utils/bin.js'; import { getStorybookConfiguration } from '../utils/storybook.js'; export default async function build(env, presetApi, options) { const sbConfigPath = getStorybookConfiguration(presetApi); return utils.process.spawn( - new URL(import.meta.resolve('storybook/index.js')).pathname, - ['build', '-c', sbConfigPath].concat(options), + 'node', + [resolveScript('storybook/index.js'), 'build', '-c', sbConfigPath].concat(options), { stdio: 'inherit', env, diff --git a/tools/scripts-core/src/scripts/build.js b/tools/scripts-core/src/scripts/build.js index c3d623bce5..5702f6dadf 100755 --- a/tools/scripts-core/src/scripts/build.js +++ b/tools/scripts-core/src/scripts/build.js @@ -1,5 +1,6 @@ import * as utils from '@talend/scripts-utils'; +import { resolveScript } from '../utils/bin.js'; import buildUMD from './build-lib-umd.js'; import buildLib from './build-lib.js'; @@ -8,8 +9,9 @@ export default async function build(env, _, options) { const packageType = utils.pkg.getPackageType(); if (packageType.isApp) { return utils.process.spawn( - utils.path.resolveBin('webpack'), + 'node', [ + resolveScript('webpack/bin/webpack.js'), '--config', utils.path.hereRelative( utils.path.getDirName(import.meta.url), diff --git a/tools/scripts-core/src/scripts/start-storybook.js b/tools/scripts-core/src/scripts/start-storybook.js index f957fb5972..fe7b867dc6 100755 --- a/tools/scripts-core/src/scripts/start-storybook.js +++ b/tools/scripts-core/src/scripts/start-storybook.js @@ -1,14 +1,15 @@ /* eslint-disable no-console */ import * as utils from '@talend/scripts-utils'; +import { resolveScript } from '../utils/bin.js'; import { getStorybookConfiguration } from '../utils/storybook.js'; export default async function start(env, presetApi, options) { const sbConfigPath = getStorybookConfiguration(presetApi); return utils.process.spawn( - new URL(import.meta.resolve('storybook/index.js')).pathname, - ['dev', '-c', sbConfigPath].concat(options), + 'node', + [resolveScript('storybook/index.js'), 'dev', '-c', sbConfigPath].concat(options), { stdio: 'inherit', env, diff --git a/tools/scripts-core/src/scripts/start.js b/tools/scripts-core/src/scripts/start.js index de83a21f08..37d4e63476 100755 --- a/tools/scripts-core/src/scripts/start.js +++ b/tools/scripts-core/src/scripts/start.js @@ -1,5 +1,6 @@ import * as utils from '@talend/scripts-utils'; +import { resolveScript } from '../utils/bin.js'; import startStorybook from './start-storybook.js'; export default async function start(env, _, options) { @@ -7,8 +8,9 @@ export default async function start(env, _, options) { if (packageType.isApp) { return utils.process.spawn( - utils.path.resolveBin('webpack'), + 'node', [ + resolveScript('webpack/bin/webpack.js'), 'serve', '--config', utils.path.hereRelative( diff --git a/tools/scripts-core/src/scripts/test.js b/tools/scripts-core/src/scripts/test.js index a46338f591..d8b375999b 100755 --- a/tools/scripts-core/src/scripts/test.js +++ b/tools/scripts-core/src/scripts/test.js @@ -3,6 +3,7 @@ import { fileURLToPath } from 'url'; import * as utils from '@talend/scripts-utils'; +import { resolveScript } from '../utils/bin.js'; import { getUserConfigFile } from '../utils/env.js'; async function testKarma(env, presetApi, options) { @@ -12,8 +13,8 @@ async function testKarma(env, presetApi, options) { const karmaConfigPath = path.join(configPath, 'karma.conf.js'); return utils.process.spawn( - new URL(import.meta.resolve('karma/bin/karma')).pathname, - ['start', karmaConfigPath].concat(options), + 'node', + [resolveScript('karma/bin/karma'), 'start', karmaConfigPath].concat(options), { stdio: 'inherit', env, @@ -33,8 +34,8 @@ export default async function test(env, presetApi, options) { getUserConfigFile('jest.config.js') || path.join(configPath, 'jest.config.js'); return utils.process.spawn( - utils.path.resolveBin('jest'), - ['--config', jestConfigPath].concat(options), + 'node', + [resolveScript('jest-cli/bin/jest'), '--config', jestConfigPath].concat(options), { stdio: 'inherit', env, diff --git a/tools/scripts-core/src/utils/bin.js b/tools/scripts-core/src/utils/bin.js new file mode 100644 index 0000000000..fc4920edb8 --- /dev/null +++ b/tools/scripts-core/src/utils/bin.js @@ -0,0 +1,16 @@ +/** + * Resolve the module script path. + * @param modName The bin module name + * @returns {*} The executable path + */ +export function resolveScript(modName) { + const filePath = import.meta.resolve(modName); + const parsedUrl = new URL(filePath); + let fileURL = parsedUrl.pathname; + + // For windows, remove the first char who is a slash + if (process.platform === 'win32') { + fileURL = fileURL.substring(1); + } + return fileURL; +}