Skip to content

Instantly share code, notes, and snippets.

@baywet
Created September 7, 2018 12:45
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save baywet/fb1dacca361a05568fcebfd162e86f9a to your computer and use it in GitHub Desktop.
private mapNpmCommand(command: string, packagesDevExact: string[],
packagesDevInexact: string[], packagesDepExact: string[], packagesDepInexact: string[],
packagesDepUn: string[], packagesDevUn: string[]): void {
const npmReduceRegex: RegExp = /npm\s+(i|un)\s+([\w\d\@\/\.-]+)\s+(-D|-S)?\s?(-E)?/gm;
const npmReduceMatch: RegExpExecArray | null = npmReduceRegex.exec(command);
const packageNameGroupId: number = 2;
const installCommandGroupId: number = 1;
const dependencyCategoryGroupId: number = 3;
const exactGroupId: number = 4;
if (npmReduceMatch) {
if (npmReduceMatch[dependencyCategoryGroupId] === '-S') {
if (npmReduceMatch[exactGroupId] === '-E') {
packagesDepExact.push(npmReduceMatch[packageNameGroupId]);
} else if (npmReduceMatch[installCommandGroupId] === 'i') {
packagesDepInexact.push(npmReduceMatch[packageNameGroupId]);
} else {
packagesDepUn.push(npmReduceMatch[packageNameGroupId]);
}
} else if (npmReduceMatch[dependencyCategoryGroupId] === '-D') {
if (npmReduceMatch[exactGroupId] === '-E') {
packagesDevExact.push(npmReduceMatch[packageNameGroupId]);
} else if (npmReduceMatch[installCommandGroupId] === 'i') {
packagesDevInexact.push(npmReduceMatch[packageNameGroupId]);
} else {
packagesDevUn.push(npmReduceMatch[packageNameGroupId]);
}
}
}
}
private reduceNpmCommand(commandsToExecute: string[], packagesDevExact: string[],
packagesDevInexact: string[], packagesDepExact: string[], packagesDepInexact: string[],
packagesDepUn: string[], packagesDevUn: string[]): void {
if (packagesDepExact.length > 0) {
commandsToExecute.push(`npm i ${packagesDepExact.join(' ')} -S -E`);
}
if (packagesDevExact.length > 0) {
commandsToExecute.push(`npm i ${packagesDevExact.join(' ')} -D -E`);
}
if (packagesDepInexact.length > 0) {
commandsToExecute.push(`npm i ${packagesDepInexact.join(' ')} -S`);
}
if (packagesDevInexact.length > 0) {
commandsToExecute.push(`npm i ${packagesDevInexact.join(' ')} -D`);
}
if (packagesDepUn.length > 0) {
commandsToExecute.push(`npm un ${packagesDepUn.join(' ')} -S`);
}
if (packagesDevUn.length > 0) {
commandsToExecute.push(`npm un ${packagesDevUn.join(' ')} -D`);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment