Skip to content

Commit

Permalink
add yarn
Browse files Browse the repository at this point in the history
  • Loading branch information
SunWuyuan committed Jan 28, 2024
1 parent b5f9cf8 commit f751b28
Show file tree
Hide file tree
Showing 8 changed files with 2,855 additions and 2,687 deletions.
22 changes: 22 additions & 0 deletions .github/workflows/npm-publish.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# This workflow will run tests using node and then publish a package to GitHub Packages when a release is created
# For more information see: https://docs.github.com/en/actions/publishing-packages/publishing-nodejs-packages

name: Package and Publish

on:
push:
branches:
- master

jobs:
publish-npm:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
node-version: 20
registry-url: https://registry.npmjs.org/
- run: npm publish
env:
NODE_AUTH_TOKEN: ${{secrets.npm_token}}
18 changes: 9 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
nrm -- NPM registry manager
wnrm -- NPM and YARN registry manager
===

[![NPM version][npm-image]][npm-url]

`nrm` can help you easy and fast switch between different npm registries,
`wnrm` can help you easy and fast switch between different npm registries,
now include: `npm`, `cnpm`, `taobao`, `nj(nodejitsu)`.

## How to configure yarn to use private registry ?
Expand All @@ -16,12 +16,12 @@ Or you can configure it in your HOME directory's .yarnrc
## Install

```
$ npm install -g nrm
$ npm install -g wnrm
```

## Example
```
$ nrm ls
$ wnrm ls
* npm ---------- https://registry.npmjs.org/
yarn --------- https://registry.yarnpkg.com/
Expand All @@ -33,7 +33,7 @@ $ nrm ls
```

```
$ nrm use cnpm //switch registry to cnpm
$ wnrm use cnpm //switch registry to cnpm
Registry has been set to: http://r.cnpmjs.org/
Expand All @@ -42,7 +42,7 @@ $ nrm use cnpm //switch registry to cnpm
## Usage

```
Usage: nrm [options] [command]
Usage: wnrm [options] [command]
Commands:
Expand Down Expand Up @@ -102,7 +102,7 @@ When you are using a custom registry you will need to run the `set-hosted-repo`

## Maintainer is wanted

If you find nrm is useful and is a experienced node.js developer, then you can help maintain nrm.
If you find wnrm is useful and is a experienced node.js developer, then you can help maintain wnrm.
If you have the interest you can reach me through email: pana.wang@outlook.com

## Contributors
Expand All @@ -113,5 +113,5 @@ If you have the interest you can reach me through email: pana.wang@outlook.com
MIT


[npm-image]: https://img.shields.io/npm/v/nrm.svg?style=flat-square
[npm-url]: https://npmjs.org/package/nrm
[npm-image]: https://img.shields.io/npm/v/wnrm.svg?style=flat-square
[npm-url]: https://npmjs.org/package/wnrm
31 changes: 29 additions & 2 deletions actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const {
exit,
readFile,
writeFile,
writeYarnFile,
geneDashLine,
printMessages,
printSuccess,
Expand All @@ -16,7 +17,7 @@ const {
isInternalRegistry,
} = require('./helpers');

const { NRMRC, NPMRC, AUTH, EMAIL, ALWAYS_AUTH, REPOSITORY, REGISTRY, HOME } = require('./constants');
const { NRMRC, NPMRC,YARNRC, AUTH, EMAIL, ALWAYS_AUTH, REPOSITORY, REGISTRY, HOME } = require('./constants');

async function onList() {
const currentRegistry = await getCurrentRegistry();
Expand Down Expand Up @@ -61,7 +62,8 @@ async function onUse(name) {
const registry = registries[name];
const npmrc = await readFile(NPMRC);
await writeFile(NPMRC, Object.assign(npmrc, registry));

const yarnrc = await readFile(YARNRC);
await writeYarnFile(YARNRC, registry);
printSuccess(`The registry has been changed to '${name}'.`);
}

Expand Down Expand Up @@ -137,6 +139,14 @@ async function onLogin(name, base64, { alwaysAuth, username, password, email })
[EMAIL]: registry[EMAIL],
}));
}
if (currentRegistry === registry[REGISTRY]) {
const yarnrc = await readFile(YARNRC);
await writeFile(YARNRC, Object.assign(yarnrc, {
[AUTH]: registry[AUTH],
[ALWAYS_AUTH]: registry[ALWAYS_AUTH],
[EMAIL]: registry[EMAIL],
}));
}
}

async function onSetRepository(name, repo) {
Expand All @@ -157,13 +167,22 @@ async function onSetRepository(name, repo) {
await writeFile(NPMRC, npmrc);
printSuccess(`Set repository attribute of npmrc successfully`);
}
if (currentRegistry && registry[REGISTRY] === currentRegistry) {
const yarnrc = await readFile(YARNRC);
Object.assign(yarnrc, { [REPOSITORY]: repo });
await writeFile(YARNRC, yarnrc);
printSuccess(`Set repository attribute of yarnrc successfully`);
}
}

async function onSetScope(scopeName, url) {
const scopeRegistryKey = `${scopeName}:${REGISTRY}`;
const npmrc = await readFile(NPMRC);
Object.assign(npmrc, { [scopeRegistryKey]: url });
await writeFile(NPMRC, npmrc);
const yarnrc = await readFile(YARNRC);
Object.assign(yarnrc, { [scopeRegistryKey]: url });
await writeFile(YARNRC, yarnrc);
printSuccess(`Set scope '${scopeRegistryKey}=${url}' success.`);
}

Expand All @@ -175,6 +194,12 @@ async function onDeleteScope(scopeName) {
await writeFile(NPMRC, npmrc);
printSuccess(`Delete scope '${scopeRegistryKey}' success.`);
}
const yarnrc = await readFile(YARNRC);
if (yarnrc[scopeRegistryKey]) {
delete yarnrc[scopeRegistryKey];
await writeFile(YARNRC, yarnrc);
printSuccess(`Delete scope '${scopeRegistryKey}' success.`);
}
}

async function onSetAttribute(name, { attr, value }) {
Expand All @@ -195,6 +220,8 @@ async function onSetAttribute(name, { attr, value }) {
if (currentRegistry === registry[REGISTRY]) {
const npmrc = await readFile(NPMRC);
await writeFile(NPMRC, Object.assign(npmrc, { [attr]: value }));
const yarnrc = await readFile(YARNRC);
await writeFile(YARNRC, Object.assign(yarnrc, { [attr]: value }));
}
}

Expand Down
2 changes: 2 additions & 0 deletions constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@ const ALWAYS_AUTH = 'always-auth';
const REGISTRY_ATTRS = [REGISTRY, HOME, AUTH, ALWAYS_AUTH];
const NRMRC = path.join(process.env[(process.platform === 'win32') ? 'USERPROFILE' : 'HOME'], '.nrmrc');
const NPMRC = path.join(process.env[(process.platform === 'win32') ? 'USERPROFILE' : 'HOME'], '.npmrc');
const YARNRC = path.join(process.env[(process.platform === 'win32') ? 'USERPROFILE' : 'HOME'], '.yarnrc');

module.exports = {
NRMRC,
NPMRC,
YARNRC,
REGISTRIES,
AUTH,
ALWAYS_AUTH,
Expand Down
13 changes: 12 additions & 1 deletion helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const ini = require('ini');
const chalk = require('chalk');
const process = require('./process');

const { NRMRC, NPMRC, REGISTRY, REGISTRIES } = require('./constants');
const { NRMRC, NPMRC,YARNRC, REGISTRY, REGISTRIES } = require('./constants');

async function readFile(file) {
return new Promise(resolve => {
Expand Down Expand Up @@ -31,6 +31,16 @@ async function writeFile(path, content) {
});
}

async function writeYarnFile(path, content) {
return new Promise(resolve => {
try {
fs.writeFileSync(path, `registry "${content.registry}"`);
resolve();
} catch (error) {
exit(error);
}
});
}
function padding(message = '', before = 1, after = 1) {
return new Array(before).fill(' ').join('') + message + new Array(after).fill(' ').join('');
}
Expand Down Expand Up @@ -103,6 +113,7 @@ module.exports = {
isLowerCaseEqual,
readFile,
writeFile,
writeYarnFile,
getRegistries,
getCurrentRegistry,
isRegistryNotFound,
Expand Down
20 changes: 11 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,26 +1,28 @@
{
"name": "nrm",
"version": "1.2.5",
"description": "NPM registry manager can help you easy and fast switch between different npm registries, now include: cnpm, taobao, nj(nodejitsu), edunpm",
"bin": "./cli.js",
"name": "wnrm",
"version": "1.0.0",
"description": "NPM and YARN registry manager can help you easy and fast switch between different npm registries, now include: cnpm, taobao, nj(nodejitsu), edunpm",
"bin": {
"wnrm": "./cli.js"
},
"repository": {
"type": "git",
"url": "git://github.com/Pana/nrm.git"
"url": "git://github.com/Sunwuyuan/wnrm.git"
},
"scripts": {
"star": "npm star nrm",
"star": "npm star wnrm",
"test": "jest"
},
"keywords": [
"NPM",
"registry"
],
"author": "Pana",
"author": "SunWuyuan",
"license": "MIT",
"bugs": {
"url": "https://github.com/Pana/nrm/issues"
"url": "https://github.com/SunWuyuan/wnrm/issues"
},
"homepage": "https://github.com/Pana/nrm",
"homepage": "https://github.com/SunWuyuan/wnrm",
"dependencies": {
"chalk": "^4.1.2",
"commander": "^8.3.0",
Expand Down
Loading

0 comments on commit f751b28

Please sign in to comment.