npmci/ts/manager.npm/index.ts

190 lines
5.8 KiB
TypeScript
Raw Normal View History

import * as plugins from './mod.plugins.js';
import * as paths from '../npmci.paths.js';
2019-08-29 18:26:23 +00:00
import { logger } from '../npmci.logging.js';
import { bash, bashNoError, nvmAvailable } from '../npmci.bash.js';
import { Npmci } from '../npmci.classes.npmci.js';
2019-08-29 18:26:23 +00:00
export class NpmciNpmManager {
public npmciRef: Npmci;
2021-10-19 01:09:50 +00:00
constructor(npmciRefArg: Npmci) {
2019-08-29 18:26:23 +00:00
this.npmciRef = npmciRefArg;
}
/**
* handle cli input
* @param argvArg
*/
2021-10-19 01:09:50 +00:00
public async handleCli(argvArg: any) {
2019-08-29 18:26:23 +00:00
if (argvArg._.length >= 2) {
const action: string = argvArg._[1];
switch (action) {
case 'install':
await this.install();
break;
2019-10-02 09:56:59 +00:00
case 'build':
2021-05-14 18:11:12 +00:00
await this.build();
break;
2019-08-29 18:26:23 +00:00
case 'prepare':
await this.prepare();
break;
case 'test':
await this.test();
break;
case 'publish':
await this.publish();
break;
default:
logger.log('error', `>>npmci npm ...<< action >>${action}<< not supported`);
process.exit(1);
}
} else {
logger.log(
'info',
`>>npmci npm ...<< cli arguments invalid... Please read the documentation.`
);
process.exit(1);
}
}
/**
* authenticates npm with token from env var
*/
public async prepare() {
const config = this.npmciRef.npmciConfig.getConfig();
let npmrcFileString: string = '';
2023-07-12 13:35:38 +00:00
await plugins.smartobject.forEachMinimatch(
2021-11-07 03:20:14 +00:00
process.env,
'NPMCI_TOKEN_NPM*',
(npmEnvArg: string) => {
const npmRegistryUrl = npmEnvArg.split('|')[0];
2022-11-02 15:43:54 +00:00
logger.log('ok', `found token for ${npmRegistryUrl}`);
2021-11-07 03:20:14 +00:00
let npmToken = npmEnvArg.split('|')[1];
if (npmEnvArg.split('|')[2] && npmEnvArg.split('|')[2] === 'plain') {
logger.log('ok', 'npm token not base64 encoded.');
} else {
logger.log('ok', 'npm token base64 encoded.');
npmToken = plugins.smartstring.base64.decode(npmToken);
}
npmrcFileString += `//${npmRegistryUrl}/:_authToken="${npmToken}"\n`;
2021-05-14 18:19:42 +00:00
}
2021-11-07 03:20:14 +00:00
);
2019-08-29 18:26:23 +00:00
logger.log('info', `setting default npm registry to ${config.npmRegistryUrl}`);
npmrcFileString += `registry=https://${config.npmRegistryUrl}\n`;
// final check
if (npmrcFileString.length > 0) {
logger.log('info', 'found one or more access tokens');
} else {
logger.log('error', 'no access token found! Exiting!');
process.exit(1);
}
// lets save it to disk
plugins.smartfile.memory.toFsSync(npmrcFileString, '/root/.npmrc');
// lets set the cache directory
await bash(`npm config set cache ${paths.NpmciCacheDir} --global `);
2019-08-29 18:26:23 +00:00
return;
}
/**
* publish a package to npm
*/
public async publish() {
const buildPublishCommand = async () => {
let npmAccessCliString = ``;
let npmRegistryCliString = ``;
let publishVerdaccioAsWell = false;
const config = this.npmciRef.npmciConfig.getConfig();
const availableRegistries: string[] = [];
2023-07-12 13:35:38 +00:00
await plugins.smartobject.forEachMinimatch(
2021-11-07 03:20:14 +00:00
process.env,
'NPMCI_TOKEN_NPM*',
(npmEnvArg: string) => {
availableRegistries.push(npmEnvArg.split('|')[0]);
}
);
2019-08-29 18:26:23 +00:00
// -> configure package access level
if (config.npmAccessLevel) {
npmAccessCliString = `--access=${config.npmAccessLevel}`;
if (config.npmAccessLevel === 'public') {
publishVerdaccioAsWell = true;
}
} else {
throw new Error('You need to set a npmAccessLevel!!!');
}
// -> configure registry url
if (config.npmRegistryUrl) {
npmRegistryCliString = `--registry=https://${config.npmRegistryUrl}`;
} else {
logger.log('error', `no registry url specified. Can't publish!`);
process.exit(1);
}
let publishCommand = `npm publish ${npmAccessCliString} ${npmRegistryCliString} `;
// publishEverywhere
if (publishVerdaccioAsWell) {
2021-05-14 18:11:12 +00:00
const verdaccioRegistry = availableRegistries.find((registryString) =>
2019-08-29 18:26:23 +00:00
registryString.startsWith('verdaccio')
);
if (verdaccioRegistry) {
logger.log(
'info',
`package is public and verdaccio registry is specified. Also publishing to Verdaccio!`
);
publishCommand = `${publishCommand} && npm publish ${npmAccessCliString} --registry=https://${verdaccioRegistry}`;
} else {
logger.log(
'error',
`This package should also be published to Verdaccio, however there is no Verdaccio registry data available!`
);
}
}
return publishCommand;
};
// -> preparing
logger.log('info', `now preparing environment:`);
this.prepare();
await bash(`npm -v`);
2022-10-11 11:38:40 +00:00
await bash(`pnpm -v`);
2019-08-29 18:26:23 +00:00
// -> build it
2019-10-02 09:56:05 +00:00
await this.install();
await this.build();
2019-08-29 18:26:23 +00:00
logger.log('success', `Nice!!! The build for the publication was successfull!`);
logger.log('info', `Lets clean up so we don't publish any packages that don't belong to us:`);
// -> clean up before we publish stuff
await bashNoError(`rm -r ./.npmci_cache`);
await bash(`rm -r ./node_modules`);
logger.log('success', `Cleaned up!:`);
// -> publish it
logger.log('info', `now invoking npm to publish the package!`);
await bash(await buildPublishCommand());
logger.log('success', `Package was successfully published!`);
}
public async install(): Promise<void> {
logger.log('info', 'now installing dependencies:');
2022-10-11 11:38:40 +00:00
await bash('pnpm install');
2019-08-29 18:26:23 +00:00
}
2019-10-02 09:56:05 +00:00
public async build(): Promise<void> {
logger.log('info', 'now building the project:');
2022-10-11 11:38:40 +00:00
await bash('pnpm run build');
2019-10-02 09:56:05 +00:00
}
2019-08-29 18:26:23 +00:00
public async test(): Promise<void> {
logger.log('info', 'now starting tests:');
2022-10-11 11:38:40 +00:00
await bash('pnpm test');
2019-08-29 18:26:23 +00:00
}
}