diff --git a/monorepo/index.js b/monorepo/index.js index 72a6993..3d18378 100644 --- a/monorepo/index.js +++ b/monorepo/index.js @@ -4,7 +4,7 @@ const fs = require('fs'); const child_process = require('child_process'); -const rootDir = __dirname; +const rootDir = __dirname.replace(/\\/g, '/'); const mainFile = rootDir + '/lib/index.js'; // Check if required modules in same monorepo are available @@ -17,18 +17,35 @@ const fileExists = (file) => { return true; }; +/** + * Get NPM command + */ +function getNPMCommand() { + const clients = ['npm', 'npm.cmd']; + for (let i = 0; i < clients.length; i++) { + const cmd = clients[i]; + const result = child_process.spawnSync(cmd, ['--version']); + if (result.status === 0) { + return cmd; + } + } + throw new Error('Cannot execute NPM commands') +} + /** * Build scripts, return imported main file on success */ function build() { return new Promise((fulfill, reject) => { + const npm = getNPMCommand(); + // List of commands to execute const commands = []; // Build script if (!fileExists(mainFile)) { commands.push({ - cmd: 'npm', + cmd: npm, args: ['run', 'build'], cwd: rootDir, }); @@ -37,7 +54,7 @@ function build() { // Install dependencies before building if (!fileExists(rootDir + '/node_modules/typescript')) { commands.unshift({ - cmd: 'npm', + cmd: npm, args: ['install'], cwd: rootDir, }); diff --git a/monorepo/src/helpers/dirs.ts b/monorepo/src/helpers/dirs.ts index 45ac9a6..0594f03 100644 --- a/monorepo/src/helpers/dirs.ts +++ b/monorepo/src/helpers/dirs.ts @@ -2,7 +2,7 @@ import fs from 'fs'; import { dirname } from 'path'; import type { PathList } from './types'; -export const fixDir = dirname(dirname(__dirname)); +export const fixDir = dirname(dirname(__dirname.replace(/\\/g, '/'))); export const rootDir = dirname(fixDir); /** @@ -26,7 +26,7 @@ export function addToPath( */ export function pathToString(path: PathList, absolute = true): string { const isAbsolute = - path.length && (path[0] === '' || path[0].slice(0, 1) === '/'); + path.length && (path[0] === '' || path[0].slice(0, 1) === '/' || path[0].indexOf(':') !== -1); return (isAbsolute ? '' : absolute ? rootDir + '/' : './') + path.join('/'); } diff --git a/monorepo/src/helpers/exec.ts b/monorepo/src/helpers/exec.ts index 7f34a62..7f9d737 100644 --- a/monorepo/src/helpers/exec.ts +++ b/monorepo/src/helpers/exec.ts @@ -2,19 +2,41 @@ import { spawnSync } from 'child_process'; import { pathToString, relativePath } from './dirs'; import { PackageInfo } from './types'; +let npm: string; + +/** + * Get NPM command + */ + function getNPMCommand(): string { + const clients = ['npm', 'npm.cmd']; + for (let i = 0; i < clients.length; i++) { + const cmd = clients[i]; + const result = spawnSync(cmd, ['--version']); + if (result.status === 0) { + return cmd; + } + } + throw new Error('Cannot execute NPM commands') +} + + /** * Run NPM command */ export function runNPMCommand(workspace: PackageInfo, params: string[]): void { + if (npm === void 0) { + npm = getNPMCommand(); + } + const cwd = pathToString(workspace.path); - console.log('\n' + relativePath(cwd) + ':', 'npm', params.join(' ')); - const result = spawnSync('npm', params, { + console.log('\n' + relativePath(cwd) + ':', npm, params.join(' ')); + const result = spawnSync(npm, params, { cwd, stdio: 'inherit', }); if (result.status !== 0) { throw new Error( - `Failed to run "npm ${params.join(' ')}" at ${relativePath(cwd)}` + `Failed to run "${npm} ${params.join(' ')}" at ${relativePath(cwd)}` ); } }