Skip to content

Instantly share code, notes, and snippets.

@mike-solomon
Created January 3, 2025 17:01
Show Gist options
  • Save mike-solomon/4b105f388395ecfe2b005b02680215c8 to your computer and use it in GitHub Desktop.
Save mike-solomon/4b105f388395ecfe2b005b02680215c8 to your computer and use it in GitHub Desktop.
Example snippet of NpmExecutor
NpmExecutor npmShellExecutor = NpmExecutorExecutionContextView.view(ctx).getNpmExecutor().withConfigurationDirectory(dir);
npmShellExecutor.init();
command.replaceAll(s -> s
.replace("${nodeModules}", nodeModules.toString())
.replace("${repoDir}", ".")
.replace("${parser}", acc.parser()));
String angularCliVersion = getAngularCliPackage(acc, ctx);
String npmrcPath = new File(dir.toString(), ".npmrc").getAbsolutePath();
List<String> installNodeGypAndNan = new ArrayList<>(Arrays.asList("npm", "install", "--userconfig", npmrcPath, "--prefix", nodeModules.toString(), "--force", "--ignore-script", "node-gyp@10", "nan@2"));
List<String> prefixedInstallAngularCli = new ArrayList<>(Arrays.asList("npm", "install", "--userconfig", npmrcPath, "--prefix", nodeModules.toString(), "--force", "--ignore-scripts", angularCliVersion));
List<String> localNpmInstallCommand = new ArrayList<>(Arrays.asList("npm", "install", "--force", "--ignore-scripts"));
try {
if (useNvmExec) {
installNodeGypAndNan.add(0, "nvm-exec");
prefixedInstallAngularCli.add(0, "nvm-exec");
localNpmInstallCommand.add(0, "nvm-exec");
command.add(0, "nvm-exec");
}
Map<String, String> environment = new HashMap<>();
environment.put("NG_DISABLE_VERSION_CHECK", "1");
environment.put("NG_CLI_ANALYTICS", "false");
environment.put("NODE_OPTIONS", "--max-old-space-size=2048");
environment.put("NODE_PATH", nodeModules.toString());
environment.put("TERM", "dumb");
// Install node-gyp to avoid issues with `npx`
npmShellExecutor.exec(installNodeGypAndNan, dir, environment, ctx);
// install angular cli in the project
npmShellExecutor.exec(prefixedInstallAngularCli, dir, environment, ctx);
// install the project dependencies
npmShellExecutor.exec(localNpmInstallCommand, dir, environment, ctx);
// run `ng update` command
Path out = npmShellExecutor.exec(command, dir, environment, ctx);
for (Map.Entry<Path, Long> entry : acc.beforeModificationTimestamps.entrySet()) {
Path path = entry.getKey();
if (!Files.exists(path) || Files.getLastModifiedTime(path).toMillis() > entry.getValue()) {
acc.modified(path);
}
}
processOutput(out, acc, ctx);
} catch (IOException e) {
throw new RuntimeException(e);
} finally {
// Restore npm settings
npmShellExecutor.postExec();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment