2
0
mirror of https://github.com/iconify/iconify.git synced 2024-12-13 14:13:06 +00:00

Fix executing NPM on Windows

This commit is contained in:
Vjacheslav Trushkin 2022-01-28 12:24:48 +02:00
parent d836e30ab9
commit d240b3975d
3 changed files with 47 additions and 8 deletions

View File

@ -4,7 +4,7 @@
const fs = require('fs'); const fs = require('fs');
const child_process = require('child_process'); const child_process = require('child_process');
const rootDir = __dirname; const rootDir = __dirname.replace(/\\/g, '/');
const mainFile = rootDir + '/lib/index.js'; const mainFile = rootDir + '/lib/index.js';
// Check if required modules in same monorepo are available // Check if required modules in same monorepo are available
@ -17,18 +17,35 @@ const fileExists = (file) => {
return true; 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 * Build scripts, return imported main file on success
*/ */
function build() { function build() {
return new Promise((fulfill, reject) => { return new Promise((fulfill, reject) => {
const npm = getNPMCommand();
// List of commands to execute // List of commands to execute
const commands = []; const commands = [];
// Build script // Build script
if (!fileExists(mainFile)) { if (!fileExists(mainFile)) {
commands.push({ commands.push({
cmd: 'npm', cmd: npm,
args: ['run', 'build'], args: ['run', 'build'],
cwd: rootDir, cwd: rootDir,
}); });
@ -37,7 +54,7 @@ function build() {
// Install dependencies before building // Install dependencies before building
if (!fileExists(rootDir + '/node_modules/typescript')) { if (!fileExists(rootDir + '/node_modules/typescript')) {
commands.unshift({ commands.unshift({
cmd: 'npm', cmd: npm,
args: ['install'], args: ['install'],
cwd: rootDir, cwd: rootDir,
}); });

View File

@ -2,7 +2,7 @@ import fs from 'fs';
import { dirname } from 'path'; import { dirname } from 'path';
import type { PathList } from './types'; import type { PathList } from './types';
export const fixDir = dirname(dirname(__dirname)); export const fixDir = dirname(dirname(__dirname.replace(/\\/g, '/')));
export const rootDir = dirname(fixDir); export const rootDir = dirname(fixDir);
/** /**
@ -26,7 +26,7 @@ export function addToPath(
*/ */
export function pathToString(path: PathList, absolute = true): string { export function pathToString(path: PathList, absolute = true): string {
const isAbsolute = 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('/'); return (isAbsolute ? '' : absolute ? rootDir + '/' : './') + path.join('/');
} }

View File

@ -2,19 +2,41 @@ import { spawnSync } from 'child_process';
import { pathToString, relativePath } from './dirs'; import { pathToString, relativePath } from './dirs';
import { PackageInfo } from './types'; 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 * Run NPM command
*/ */
export function runNPMCommand(workspace: PackageInfo, params: string[]): void { export function runNPMCommand(workspace: PackageInfo, params: string[]): void {
if (npm === void 0) {
npm = getNPMCommand();
}
const cwd = pathToString(workspace.path); const cwd = pathToString(workspace.path);
console.log('\n' + relativePath(cwd) + ':', 'npm', params.join(' ')); console.log('\n' + relativePath(cwd) + ':', npm, params.join(' '));
const result = spawnSync('npm', params, { const result = spawnSync(npm, params, {
cwd, cwd,
stdio: 'inherit', stdio: 'inherit',
}); });
if (result.status !== 0) { if (result.status !== 0) {
throw new Error( throw new Error(
`Failed to run "npm ${params.join(' ')}" at ${relativePath(cwd)}` `Failed to run "${npm} ${params.join(' ')}" at ${relativePath(cwd)}`
); );
} }
} }