2
0
mirror of https://github.com/iconify/iconify.git synced 2024-12-12 05:37:49 +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 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,
});

View File

@ -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('/');
}

View File

@ -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)}`
);
}
}