Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

simple-aznpmrcs

Package Overview
Dependencies
Maintainers
1
Versions
17
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

simple-aznpmrcs - npm Package Compare versions

Comparing version 0.2.4 to 0.3.0

lib/helpers/npmrcs/init.d.ts

14

CHANGELOG.md

@@ -7,5 +7,17 @@ # Changelog

## [0.3.0] - 2023-07-05
### Added
- `List` command to show all available npmrcs
- `Delete` command to delete one
- `Use` command that gives a list of all available npmrcs and you can chose which one you want to activate.
### Changed
- No stacktrace is shown anymore per default
- Use can be executed without name, it will default to current working directory name
- Update can be executed without name, it will default to current working directory name
### Fixed
- When calling when no npmrc_store available it will first initialize it.
## [0.2.4] - 2023-07-04
### Added
- use command
- Use command

@@ -12,0 +24,0 @@ ## [0.2.3] - 2023-07-04

8

lib/helpers/npmrc.d.ts

@@ -1,4 +0,6 @@

declare function useNpmrc(name: string): void;
declare function deleteNpmrc(name: string): void;
declare function listNpmrcs(): void;
declare function useNpmrc(name?: string): void;
declare function createNpmrcs(feed: string, azProject?: string, azOrg?: string, name?: string): void;
declare function updateNpmrcWithNewPat(npmrcName?: string): void;
export { updateNpmrcWithNewPat, createNpmrcs, useNpmrc, };
declare function updateNpmrcWithNewPat(npmrcName?: string, all?: boolean): void;
export { updateNpmrcWithNewPat, createNpmrcs, useNpmrc, deleteNpmrc, listNpmrcs, };
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.useNpmrc = exports.createNpmrcs = exports.updateNpmrcWithNewPat = void 0;
exports.listNpmrcs = exports.deleteNpmrc = exports.useNpmrc = exports.createNpmrcs = exports.updateNpmrcWithNewPat = void 0;
const fs = require("fs");
const child_process_1 = require("child_process");
const path_1 = require("path");
const azureDevopsAPI_1 = require("./azureDevopsAPI");
//@ts-ignore
const NPMRC_STORE = process.env.NPMRC_STORE || (0, path_1.join)(process.env.HOME || process.env.USERPROFILE, '.npmrcs');
//@ts-ignore
const NPMRC = process.env.NPMRC || (0, path_1.join)(process.env.HOME || process.env.USERPROFILE, '.npmrc');
const init_1 = require("./npmrcs/init");
function deleteNpmrc(name) {
console.log('Deleting', name);
if (fs.existsSync(`${init_1.NPMRC_STORE}/${name}`)) {
fs.unlinkSync(`${init_1.NPMRC_STORE}/${name}`);
console.log(`NPMRC with name ${name} is deleted.`);
}
else {
throw new Error(`Provided npmrc(${name}) does not exist.`);
}
}
exports.deleteNpmrc = deleteNpmrc;
function listNpmrcs() {
const currentSelectedNpmrc = fs.readlinkSync(init_1.NPMRC);
fs.readdirSync(init_1.NPMRC_STORE).forEach(function (npmrc) {
if (npmrc[0] !== '.') {
console.log(' %s %s', (0, path_1.basename)(currentSelectedNpmrc) == npmrc ? '*' : ' ', npmrc);
}
});
}
exports.listNpmrcs = listNpmrcs;
function useNpmrc(name) {
if (!fs.existsSync(`${NPMRC_STORE}/${name}`)) {
if (!name) {
// If there is no name we use the current directory name as name.
name = (0, path_1.basename)((0, path_1.resolve)());
}
if (!fs.existsSync(`${init_1.NPMRC_STORE}/${name}`)) {
console.log('Provided NPMRC does not exists yet. Please create it first.');
throw new Error('Provided NPMRC does not exists yet. Please create it first.');
}
(0, child_process_1.execSync)(`npx npmrc ${name}`, { stdio: 'inherit' });
(0, init_1.switchNpmrc)(name);
}

@@ -52,5 +72,10 @@ exports.useNpmrc = useNpmrc;

const template = fs.readFileSync((0, path_1.join)(__dirname, '../../assets/npmrc-azure-feeds')).toString();
if (!fs.existsSync(`${NPMRC_STORE}/${name}`)) {
// Create an npmrc with the name of the project
(0, child_process_1.execSync)(`npx -y npmrc -c ${name}`, { stdio: 'inherit' });
if (!fs.existsSync(`${init_1.NPMRC_STORE}`)) {
// npmrc_store does not exist, we need to create it first.
console.log('Initializing NPMRC store and creating default npmrc based on current');
(0, init_1.makeStore)();
console.log('Your previous NPMRC is now available under the name: default');
}
if (!fs.existsSync(`${init_1.NPMRC_STORE}/${name}`)) {
console.log('Creating NPMRC', name);
// Now we update the template with the feed.

@@ -61,23 +86,29 @@ let newNpmrc = template.replaceAll('$NPM_FEED', feed);

// Save the npmrc so that it can be used.
fs.writeFileSync(`${NPMRC_STORE}/${name}`, newNpmrc);
fs.writeFileSync(`${init_1.NPMRC_STORE}/${name}`, newNpmrc);
}
updateNpmrcWithNewPat(name);
useNpmrc(name);
}
exports.createNpmrcs = createNpmrcs;
function updateNpmrcWithNewPat(npmrcName) {
function updateNpmrcWithNewPat(npmrcName, all) {
// NPMRC file exists let's update it with a new password.
let files;
if (npmrcName) {
let files = [];
if (!npmrcName && !all) {
const currentSelectedNpmrc = fs.readlinkSync(init_1.NPMRC);
console.log('Updating currently selected NPMRC', (0, path_1.basename)(currentSelectedNpmrc));
files = [(0, path_1.basename)(currentSelectedNpmrc)];
}
else if (npmrcName && !all) {
console.log('Updating provided npmrc:', npmrcName);
files = [npmrcName];
}
else {
else if (all) {
console.log('Updating all npmrcs');
files = fs.readdirSync(`${NPMRC_STORE}`);
files = fs.readdirSync(`${init_1.NPMRC_STORE}`);
}
files.map((file) => {
// check if file exists, else throw warning.
if (fs.existsSync(`${NPMRC_STORE}/${file}`)) {
if (fs.existsSync(`${init_1.NPMRC_STORE}/${file}`)) {
// Load the npmrc file
let npmrc = fs.readFileSync(`${NPMRC_STORE}/${file}`).toString();
let npmrc = fs.readFileSync(`${init_1.NPMRC_STORE}/${file}`).toString();
// Determine az organization

@@ -106,3 +137,3 @@ const registryUrl = npmrc.substring(npmrc.indexOf('registry=https://pkgs.dev.azure.com/') + 9, npmrc.indexOf('\n'));

npmrc = npmrc + `#simple-aznpmrcs#${authorizationId}#simple-aznpmrcs#`;
fs.writeFileSync(`${NPMRC_STORE}/${file}`, npmrc);
fs.writeFileSync(`${init_1.NPMRC_STORE}/${file}`, npmrc);
console.log('NPMRC is updated with a fresh new PAT, happy coding!');

@@ -109,0 +140,0 @@ }

@@ -5,3 +5,31 @@ #!/usr/bin/env node

const yargs = require("yargs");
const select_1 = require("@inquirer/select");
const npmrc_1 = require("./helpers/npmrc");
const fs_1 = require("fs");
const init_1 = require("./helpers/npmrcs/init");
const selectNPMRC = async () => {
const files = (0, fs_1.readdirSync)(`${init_1.NPMRC_STORE}`);
let choices = [];
files.forEach((file) => {
if (!file.startsWith('.')) {
choices.push({
name: file,
value: file,
description: `NPMRC for ${file}`
});
}
});
const answer = await (0, select_1.default)({
message: 'Select the NPMRC that you want to activate',
choices,
});
try {
console.log('ddd', answer);
(0, npmrc_1.useNpmrc)(answer);
}
catch (exception) {
console.log('d', exception);
console.log('Execution failed, please have a look at the error messages and help.');
}
};
const argv = yargs(process.argv.slice(2))

@@ -14,13 +42,52 @@ //@ts-ignore

Chose one of the commands that you need.`)
.command(['update [npmrc]'], 'Update provided NPMRC with new credentials, or all when no npmrc was provided.', {}, (argv) => {
(0, npmrc_1.updateNpmrcWithNewPat)(argv.npmrc);
.command(['update all'], 'Update all available NPMRC', {}, (argv) => {
try {
(0, npmrc_1.updateNpmrcWithNewPat)(undefined, true);
}
catch (exception) {
console.log('Execution failed, please have a look at the error messages and help.');
}
})
.command(['create [feed] [azOrganization] [azProject] [name]'], 'Create NPMRC with az and feed details. Name is optional, default is directory name.', {}, (argv) => {
(0, npmrc_1.createNpmrcs)(argv.feed, argv.azProject, argv.azOrganization, argv.name);
.command(['update [npmrc]'], 'Update provided NPMRC with new credentials, when no parameter is provided current active one will be updated.', {}, (argv) => {
try {
(0, npmrc_1.updateNpmrcWithNewPat)(argv.npmrc);
}
catch (exception) {
console.log('Execution failed, please have a look at the error messages and help.');
}
})
.command(['use [npmrc]'], 'Activate provided NPMRC', {}, (argv) => {
console.log('dd', argv.npmrc);
(0, npmrc_1.useNpmrc)(argv.npmrc);
.command(['create [feed] [azOrganization] [azProject] [name]'], `Create NPMRC with az and feed details. \n if azOrganization or azProject is not provided, repository url in package.json will be used to determine them. \n Name is optional, default is directory name.`, {}, (argv) => {
try {
(0, npmrc_1.createNpmrcs)(argv.feed, argv.azProject, argv.azOrganization, argv.name);
}
catch (exception) {
console.log('Execution failed, please have a look at the error messages and help.');
}
})
.command(['delete [npmrc]'], 'Delete provided NPMRC', {}, (argv) => {
try {
(0, npmrc_1.deleteNpmrc)(argv.npmrc);
}
catch (exception) {
console.log('Execution failed, please have a look at the error messages and help.');
}
})
.command(['list'], 'List all available NPMRC', {}, (argv) => {
try {
(0, npmrc_1.listNpmrcs)();
}
catch (exception) {
console.log('Execution failed, please have a look at the error messages and help.');
}
})
.command(['$0 [npmrc]'], 'Activate provided NPMRC', {}, (argv) => {
try {
(0, npmrc_1.useNpmrc)(argv.npmrc);
}
catch (exception) {
console.log('Execution failed, please have a look at the error messages and help.');
}
})
.command('use', 'Select an NPMRC to be used', () => { }, selectNPMRC)
.help()
.parseAsync();
.argv;
{
"name": "simple-aznpmrcs",
"version": "0.2.4",
"version": "0.3.0",
"description": "CLI to help with setting up feeds and authentication for Azure DevOps",

@@ -20,2 +20,3 @@ "bin": {

"dependencies": {
"@inquirer/select": "^1.2.3",
"yargs": "^17.7.2"

@@ -26,8 +27,8 @@ },

"@types/node": "^20.3.2",
"typescript": "^5.0.2",
"@types/yargs": "^17.0.24"
"@types/yargs": "^17.0.24",
"typescript": "^5.0.2"
},
"bundledDependencies": [
"bundleDependencies": [
"yargs"
]
}

@@ -36,10 +36,13 @@ # Simple-aznpmrcs

For creating a NPMRC a few parameters are required:
- azOrganization (first part of url)
- azProject (second part of url)
For creating a NPMRC one parameter is required:
- feedName (as available in Artifacts)
- Optional: name (directory name is used if not provided)
*Note: azOrganization & azProject are optional if repository url is provided in your package.json*
One that have a default:
- name (directory name is used if not provided)
And two that are needed but can be retrieved from a different property.
When `repository.url` is set in the package.json, that one will be used.
- azOrganization (first part of Azure DevOps url)
- azProject (second part of Azure DevOps url)
Example Azure DevOps url: `https://dev.azure.com/henkvandenbrink/kitchensink`

@@ -50,22 +53,44 @@ azOrganization = `henkvandenbrink`

Example:
Examples:
`npx -y simple-aznpmrcs create henkvandenbrink kitchensink npm-feed`
Easiest:
`npx -y simple-aznmprcs create feed-name`
- This will generate an NPMRC with feed-name, the azure details are retrieved from package.json and current directory name is used as name.
More detailed:
`npx -y simple-aznpmrcs create npm-feed henkvandenbrink kitchensink myNpmrc`
- Here all options are provided and will be used.
### Switch
When you have create one or more npmrcs you can easily switch:
`npx -y simple-aznpmrcs example-npmrc`
Or if you created the NPMRC with the name of the directory:
`npx -t simple-aznpmrcs`
### Use
When you have create one or more npmrcs you can easily switch:
When issuing the `use` command a list of all available NPMRC' are shown and you can select one.
`npx -y simple-aznpmrcs use example-npmrc`
`npx -y simple-aznpmrcs use`
### Update
For updating you only need to provide the name of the NPMRC that was created earlier.
There are three options for updating:
`npx -y simple-aznpmrcs update example-npmrc`
- All
- Provide a name
- Current active npmrc
## Thanks
Update all npmrcs:
`npx -y simple-aznpmrcs update all`
This module uses [npmrc](https://github.com/deoxxa/npmrc) npm module to create and switch the npmrc.
Update provided NPMRC:
`npx -y simple-aznpmrcs myNpmrcs`
Update current NPMRC:
`npx -y simple-aznpmrcs`

@@ -6,10 +6,29 @@

import { createPat, revokePat } from './azureDevopsAPI';
import { NPMRC, NPMRC_STORE, makeStore, switchNpmrc } from './npmrcs/init';
//@ts-ignore
const NPMRC_STORE = process.env.NPMRC_STORE || join(process.env.HOME || process.env.USERPROFILE, '.npmrcs')
//@ts-ignore
const NPMRC = process.env.NPMRC || join(process.env.HOME || process.env.USERPROFILE, '.npmrc')
function deleteNpmrc(name: string) {
console.log('Deleting', name);
if (fs.existsSync(`${NPMRC_STORE}/${name}`)) {
fs.unlinkSync(`${NPMRC_STORE}/${name}`);
console.log(`NPMRC with name ${name} is deleted.`);
} else {
throw new Error(`Provided npmrc(${name}) does not exist.`);
}
}
function useNpmrc(name: string) {
function listNpmrcs() {
const currentSelectedNpmrc = fs.readlinkSync(NPMRC);
fs.readdirSync(NPMRC_STORE).forEach(function (npmrc) {
if (npmrc[0] !== '.') {
console.log(' %s %s', basename(currentSelectedNpmrc) == npmrc ? '*' : ' ', npmrc)
}
})
}
function useNpmrc(name?: string) {
if (!name) {
// If there is no name we use the current directory name as name.
name = basename(resolve());
}
if (!fs.existsSync(`${NPMRC_STORE}/${name}`)) {

@@ -19,3 +38,3 @@ console.log('Provided NPMRC does not exists yet. Please create it first.');

}
execSync(`npx npmrc ${name}`, { stdio: 'inherit' });
switchNpmrc(name);
}

@@ -57,6 +76,11 @@

if (!fs.existsSync(`${NPMRC_STORE}`)) {
// npmrc_store does not exist, we need to create it first.
console.log('Initializing NPMRC store and creating default npmrc based on current');
makeStore();
console.log('Your previous NPMRC is now available under the name: default');
}
if (!fs.existsSync(`${NPMRC_STORE}/${name}`)) {
// Create an npmrc with the name of the project
execSync(`npx -y npmrc -c ${name}`, { stdio: 'inherit' });
console.log('Creating NPMRC', name);
// Now we update the template with the feed.

@@ -70,12 +94,17 @@ let newNpmrc = template.replaceAll('$NPM_FEED', feed);

updateNpmrcWithNewPat(name);
useNpmrc(name);
}
function updateNpmrcWithNewPat(npmrcName?: string) {
function updateNpmrcWithNewPat(npmrcName?: string, all?: boolean) {
// NPMRC file exists let's update it with a new password.
let files;
let files: string[] = [];
if (npmrcName) {
if (!npmrcName && !all) {
const currentSelectedNpmrc = fs.readlinkSync(NPMRC);
console.log('Updating currently selected NPMRC', basename(currentSelectedNpmrc));
files = [basename(currentSelectedNpmrc)];
} else if (npmrcName && !all) {
console.log('Updating provided npmrc:', npmrcName);
files = [npmrcName];
} else {
} else if (all) {
console.log('Updating all npmrcs');

@@ -135,2 +164,4 @@ files = fs.readdirSync(`${NPMRC_STORE}`);

useNpmrc,
deleteNpmrc,
listNpmrcs,
}
#!/usr/bin/env node
import * as yargs from 'yargs';
import { createNpmrcs, updateNpmrcWithNewPat, useNpmrc } from './helpers/npmrc';
import select, { Separator } from '@inquirer/select';
import { createNpmrcs, deleteNpmrc, listNpmrcs, updateNpmrcWithNewPat, useNpmrc } from './helpers/npmrc';
import { readdirSync } from 'fs';
import { NPMRC_STORE } from './helpers/npmrcs/init';
const selectNPMRC = async () => {
const files = readdirSync(`${NPMRC_STORE}`);
let choices: { name: string; value: string; description: string; }[] = [];
files.forEach((file) => {
if (!file.startsWith('.')) {
choices.push({
name: file,
value: file,
description: `NPMRC for ${file}`
})
}
});
const answer = await select({
message: 'Select the NPMRC that you want to activate',
choices,
});
try {
console.log('ddd', answer)
useNpmrc(answer);
} catch (exception) {
console.log('d', exception)
console.log('Execution failed, please have a look at the error messages and help.');
}
};
const argv = yargs(process.argv.slice(2))

@@ -12,14 +46,48 @@ //@ts-ignore

Chose one of the commands that you need.`)
.command(['update [npmrc]'], 'Update provided NPMRC with new credentials, or all when no npmrc was provided.', {}, (argv) => {
updateNpmrcWithNewPat(argv.npmrc as string);
.command(['update all'], 'Update all available NPMRC', {}, (argv) => {
try {
updateNpmrcWithNewPat(undefined, true);
} catch (exception) {
console.log('Execution failed, please have a look at the error messages and help.');
}
})
.command(['create [feed] [azOrganization] [azProject] [name]'], 'Create NPMRC with az and feed details. Name is optional, default is directory name.', {}, (argv) => {
createNpmrcs(argv.feed as string, argv.azProject as string, argv.azOrganization as string, argv.name as string);
.command(['update [npmrc]'], 'Update provided NPMRC with new credentials, when no parameter is provided current active one will be updated.', {}, (argv) => {
try {
updateNpmrcWithNewPat(argv.npmrc as string);
} catch (exception) {
console.log('Execution failed, please have a look at the error messages and help.');
}
})
.command(['use [npmrc]'], 'Activate provided NPMRC', {}, (argv) => {
console.log('dd', argv.npmrc)
useNpmrc(argv.npmrc as string)
.command(['create [feed] [azOrganization] [azProject] [name]'], `Create NPMRC with az and feed details. \n if azOrganization or azProject is not provided, repository url in package.json will be used to determine them. \n Name is optional, default is directory name.`, {}, (argv) => {
try {
createNpmrcs(argv.feed as string, argv.azProject as string, argv.azOrganization as string, argv.name as string);
} catch (exception) {
console.log('Execution failed, please have a look at the error messages and help.');
}
})
.command(['delete [npmrc]'], 'Delete provided NPMRC', {}, (argv) => {
try {
deleteNpmrc(argv.npmrc as string);
} catch (exception) {
console.log('Execution failed, please have a look at the error messages and help.');
}
})
.command(['list'], 'List all available NPMRC', {}, (argv) => {
try {
listNpmrcs();
} catch (exception) {
console.log('Execution failed, please have a look at the error messages and help.');
}
})
.command(['$0 [npmrc]'], 'Activate provided NPMRC', {}, (argv) => {
try {
useNpmrc(argv.npmrc as string);
} catch (exception) {
console.log('Execution failed, please have a look at the error messages and help.');
}
})
.command('use', 'Select an NPMRC to be used', () => { }, selectNPMRC)
.help()
.parseAsync();
.argv;

@@ -29,1 +97,3 @@

{
"compilerOptions": {
"target": "es2022",
"module": "CommonJS",
"esModuleInterop": false,
"resolveJsonModule": true,
"moduleResolution": "node",
"allowSyntheticDefaultImports" : true,
"lib": ["dom", "es2022"],
"declaration": true,
"outDir": "lib",
"rootDir": "src",
"strict": true,
"types": ["node"],
},
"exclude": [
"lib",
"node_modules",
"src/**/*.spec.ts",
"bin"
"compilerOptions": {
"target": "es2022",
"module": "CommonJS",
"esModuleInterop": false,
"resolveJsonModule": true,
"moduleResolution": "node",
"allowSyntheticDefaultImports": true,
"lib": [
"dom",
"es2022"
],
"include": [
"src", "src/__mocks__"
]
}
"declaration": true,
"outDir": "lib",
"rootDir": "src",
"strict": true,
"skipLibCheck": true,
"types": [
"node",
],
},
"exclude": [
"lib",
"node_modules",
"src/**/*.spec.ts",
"bin"
],
"include": [
"src",
"src/__mocks__"
]
}
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc