Use .npmrc file created by ci pipeline

This commit is contained in:
Dan Schulte 2018-09-06 10:08:41 -07:00
Родитель f148f70e49
Коммит dd3e46d217
1 изменённых файлов: 8 добавлений и 63 удалений

Просмотреть файл

@ -7,15 +7,11 @@
const gulp = require('gulp');
const args = require('yargs').argv;
const fs = require('fs');
const util = require('util');
const path = require('path');
const glob = require('glob');
const execSync = require('child_process').execSync;
const jsonStableStringify = require('json-stable-stringify');
const azureSDKForNodeRepoRoot = __dirname;
const defaultAutoRestVersion = '1.2.2';
var usingAutoRestVersion;
const azureRestAPISpecsRoot = args['azure-rest-api-specs-root'] || path.resolve(azureSDKForNodeRepoRoot, '..', 'azure-rest-api-specs');
const package = args['package'];
const use = args['use'];
@ -40,15 +36,7 @@ function getServiceNameFromOutputFolderValue(outputFolderValue) {
return outputFolderSegments[outputFolderSegments.length - 1];
}
function npmInstall(packageFolderPath) {
execSync(`npm install`, { cwd: packageFolderPath, stdio: ['ignore', 'ignore', 'pipe'] });
}
gulp.task('default', function () {
console.log('gulp install --package <package name>');
console.log(' --package');
console.log(' NPM package to run "npm install" on.');
console.log();
console.log('gulp codegen [--azure-rest-api-specs-root <azure-rest-api-specs root>] [--use <autorest.nodejs root>] [--package <package name>]');
console.log(' --azure-rest-api-specs-root');
console.log(' Root location of the local clone of the azure-rest-api-specs-root repository.');
@ -62,37 +50,6 @@ gulp.task('default', function () {
console.log(' The name of the package to publish. If no package is specified, then all packages will be published.');
});
gulp.task("install", function () {
if (!package) {
console.log(`No --package specified to run "npm install" on.`);
} else {
const nodejsReadmeFilePaths = findReadmeNodejsMdFilePaths(azureRestAPISpecsRoot);
let foundPackage = false;
for (let i = 0; i < nodejsReadmeFilePaths.length; ++i) {
const nodejsReadmeFilePath = nodejsReadmeFilePaths[i];
const nodejsReadmeFileContents = fs.readFileSync(nodejsReadmeFilePath, 'utf8');
const packageName = getPackageNameFromReadmeNodejsMdFileContents(nodejsReadmeFileContents);
if (package === packageName || packageName.endsWith(`-${package}`)) {
foundPackage = true;
const outputFolderPath = getOutputFolderFromReadmeNodeJsMdFileContents(nodejsReadmeFileContents);
const outputFolderPathRelativeToAzureSDKForNodeRepoRoot = outputFolderPath.substring('$(node-sdks-folder)/'.length);
const packageFolderPath = path.resolve(azureSDKForNodeRepoRoot, outputFolderPathRelativeToAzureSDKForNodeRepoRoot);
console.log(`[${packageFolderPath}]> npm install`);
npmInstall(packageFolderPath);
}
}
if (!foundPackage) {
console.log(`No package found with the name "${package}".`);
}
}
});
//This task is used to generate libraries based on the mappings specified above.
gulp.task('codegen', function () {
const nodejsReadmeFilePaths = findReadmeNodejsMdFilePaths(azureRestAPISpecsRoot);
@ -127,12 +84,6 @@ gulp.task('codegen', function () {
const result = execSync(cmd, { encoding: 'utf8' });
console.log('Output:');
console.log(result);
console.log('Installing dependencies...');
const outputFolderPath = getOutputFolderFromReadmeNodeJsMdFileContents(nodejsReadmeFileContents);
const outputFolderPathRelativeToAzureSDKForNodeRepoRoot = outputFolderPath.substring('$(node-sdks-folder)/'.length);
const packageFolderPath = path.resolve(azureSDKForNodeRepoRoot, outputFolderPathRelativeToAzureSDKForNodeRepoRoot);
npmInstall(packageFolderPath);
} catch (err) {
console.log('Error:');
console.log(`An error occurred while generating client for package: "${packageName}":\n ${err.stderr}`);
@ -278,15 +229,14 @@ gulp.task('publish', (cb) => {
let publishedPackages = 0;
let publishedPackagesSkipped = 0;
const npmAuth = process.env["npm-auth"];
if (npmAuth) {
console.log(`Found 'npm-auth' environment variable. Using it to authenticate with NPM for publish.`);
const npmrcRootFilePath = "./.npmrc";
const npmrcRootFileExists = fs.exists(npmrcRootFilePath);
if (npmrcRootFileExists) {
console.log(`Found ".npmrc" auth file in repository root. Using it to authenticate with NPM for publish.`);
}
for (let i = 0; i < nodejsReadmeFilePaths.length; ++i) {
const nodejsReadmeFilePath = nodejsReadmeFilePaths[i];
// console.log(`INFO: Processing ${nodejsReadmeFilePath}`);
const nodejsReadmeFileContents = fs.readFileSync(nodejsReadmeFilePath, 'utf8');
const relativeOutputFolderPath = nodejsReadmeFileContents.match(/output\-folder: \$\(node\-sdks\-folder\)\/(lib\/services\/\S+)/)[1];
const packageFolderPath = path.resolve(azureSDKForNodeRepoRoot, relativeOutputFolderPath);
@ -327,17 +277,12 @@ gulp.task('publish', (cb) => {
console.log(`Publishing package "${packageName}" with version "${localPackageVersion}"...${whatif ? " (SKIPPED)" : ""}`);
if (!whatif) {
try {
npmInstall(packageFolderPath);
const npmrcFilePath = path.join(packageFolderPath, ".npmrc");
if (npmAuth) {
console.log(`Writing npm-auth value to "${npmrcFilePath}".`);
fs.writeFileSync(npmrcFilePath, npmAuth);
const npmrcPackageFilePath = path.join(packageFolderPath, npmrcRootFilePath);
if (npmrcRootFileExists) {
console.log(`Copying "${npmrcRootFilePath}" to "${npmrcPackageFilePath}".`);
fs.copyFileSync(npmrcRootFilePath, npmrcPackageFilePath);
}
execSync(`npm publish --access public`, { cwd: packageFolderPath });
if (npmAuth) {
console.log(`Deleting npm-auth value at "${npmrcFilePath}".`);
fs.unlinkSync(npmrcFilePath);
}
publishedPackages++;
}
catch (error) {