Skip to content

Commit

Permalink
fix(script-core): make a solution compatible with windows and pnpm (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
smouillour authored Dec 5, 2023
1 parent ec2c6f2 commit 55f1d54
Show file tree
Hide file tree
Showing 9 changed files with 51 additions and 15 deletions.
5 changes: 5 additions & 0 deletions .changeset/lazy-badgers-shave.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@talend/scripts-core': patch
---

fix(script-core): make a solution compatible with windows and pnpm
9 changes: 7 additions & 2 deletions tools/scripts-core/src/scripts/build-lib-umd.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 },
);
}
Expand Down
9 changes: 6 additions & 3 deletions tools/scripts-core/src/scripts/build-lib.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -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',
Expand Down Expand Up @@ -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) {
Expand Down
5 changes: 3 additions & 2 deletions tools/scripts-core/src/scripts/build-storybook.js
Original file line number Diff line number Diff line change
@@ -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,
Expand Down
4 changes: 3 additions & 1 deletion tools/scripts-core/src/scripts/build.js
Original file line number Diff line number Diff line change
@@ -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';

Expand All @@ -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),
Expand Down
5 changes: 3 additions & 2 deletions tools/scripts-core/src/scripts/start-storybook.js
Original file line number Diff line number Diff line change
@@ -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,
Expand Down
4 changes: 3 additions & 1 deletion tools/scripts-core/src/scripts/start.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
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) {
const packageType = utils.pkg.getPackageType();

if (packageType.isApp) {
return utils.process.spawn(
utils.path.resolveBin('webpack'),
'node',
[
resolveScript('webpack/bin/webpack.js'),
'serve',
'--config',
utils.path.hereRelative(
Expand Down
9 changes: 5 additions & 4 deletions tools/scripts-core/src/scripts/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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,
Expand All @@ -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,
Expand Down
16 changes: 16 additions & 0 deletions tools/scripts-core/src/utils/bin.js
Original file line number Diff line number Diff line change
@@ -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;
}

0 comments on commit 55f1d54

Please sign in to comment.