Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: add prettier to format code #190

Merged
merged 1 commit into from
Nov 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/test-Windows.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: Unit Test(Windows)
on:
on:
pull_request:
branches: [master]
jobs:
Expand Down Expand Up @@ -29,4 +29,4 @@ jobs:
run: yarn install

- name: Test
run: yarn run test
run: yarn run test
4 changes: 2 additions & 2 deletions .github/workflows/test-macOS.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: Unit Test(macOS)
on:
on:
pull_request:
branches: [master]
jobs:
Expand Down Expand Up @@ -29,4 +29,4 @@ jobs:
run: yarn install

- name: Test
run: yarn run test
run: yarn run test
3 changes: 3 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
*.md
.changeset
package.json
90 changes: 50 additions & 40 deletions actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ async function onList() {
const currentRegistry = await getCurrentRegistry();
const registries = await getRegistries();
const keys = Object.keys(registries);
const length = Math.max(...keys.map(key => key.length)) + 3;
const length = Math.max(...keys.map((key) => key.length)) + 3;

const messages = keys.map(key => {
const messages = keys.map((key) => {
const registry = registries[key];
const prefix = isLowerCaseEqual(registry[REGISTRY], currentRegistry) ? chalk.green.bold('* ') : ' ';
return prefix + key + geneDashLine(key, length) + registry[REGISTRY];
Expand Down Expand Up @@ -65,7 +65,7 @@ async function onUse(name) {
}

async function onDelete(name) {
if (await isRegistryNotFound(name) || await isInternalRegistry(name, 'delete')) {
if ((await isRegistryNotFound(name)) || (await isInternalRegistry(name, 'delete'))) {
return;
}

Expand All @@ -84,9 +84,11 @@ async function onDelete(name) {
async function onAdd(name, url, home) {
const registries = await getRegistries();
const registryNames = Object.keys(registries);
const registryUrls = registryNames.map(name => registries[name][REGISTRY]);
if (registryNames.includes(name) || registryUrls.some(eachUrl => isLowerCaseEqual(eachUrl, url))) {
return exit('The registry name or url is already included in the nrm registries. Please make sure that the name and url are unique.');
const registryUrls = registryNames.map((name) => registries[name][REGISTRY]);
if (registryNames.includes(name) || registryUrls.some((eachUrl) => isLowerCaseEqual(eachUrl, url))) {
return exit(
'The registry name or url is already included in the nrm registries. Please make sure that the name and url are unique.',
);
}

const newRegistry = {};
Expand All @@ -101,7 +103,7 @@ async function onAdd(name, url, home) {
}

async function onLogin(name, base64, { alwaysAuth, username, password, email }) {
if (await isRegistryNotFound(name) || await isInternalRegistry(name, 'set authorization information of')) {
if ((await isRegistryNotFound(name)) || (await isInternalRegistry(name, 'set authorization information of'))) {
return;
}

Expand Down Expand Up @@ -130,16 +132,19 @@ async function onLogin(name, base64, { alwaysAuth, username, password, email })
const currentRegistry = await getCurrentRegistry();
if (currentRegistry === registry[REGISTRY]) {
const npmrc = await readFile(NPMRC);
await writeFile(NPMRC, Object.assign(npmrc, {
[AUTH]: registry[AUTH],
[ALWAYS_AUTH]: registry[ALWAYS_AUTH],
[EMAIL]: registry[EMAIL],
}));
await writeFile(
NPMRC,
Object.assign(npmrc, {
[AUTH]: registry[AUTH],
[ALWAYS_AUTH]: registry[ALWAYS_AUTH],
[EMAIL]: registry[EMAIL],
}),
);
}
}

async function onSetRepository(name, repo) {
if (await isRegistryNotFound(name) || await isInternalRegistry(name, 'set repository of')) {
if ((await isRegistryNotFound(name)) || (await isInternalRegistry(name, 'set repository of'))) {
return;
}

Expand Down Expand Up @@ -177,7 +182,7 @@ async function onDeleteScope(scopeName) {
}

async function onSetAttribute(name, { attr, value }) {
if (await isRegistryNotFound(name) || await isInternalRegistry(name, 'set attribute of')) {
if ((await isRegistryNotFound(name)) || (await isInternalRegistry(name, 'set attribute of'))) {
return;
}

Expand All @@ -198,14 +203,14 @@ async function onSetAttribute(name, { attr, value }) {
}

async function onRename(name, newName) {
if (await isRegistryNotFound(name) || await isInternalRegistry(name, 'rename')) {
if ((await isRegistryNotFound(name)) || (await isInternalRegistry(name, 'rename'))) {
return;
}
if (name === newName) {
return exit('The names cannot be the same.');
}

if (!await isRegistryNotFound(newName, false)) {
if (!(await isRegistryNotFound(newName, false))) {
return exit(`The new registry name '${newName}' is already exist.`);
}
const customRegistries = await readFile(NRMRC);
Expand All @@ -231,43 +236,48 @@ async function onTest(target) {
const registries = await getRegistries();
const timeout = 5000;

if (target && await isRegistryNotFound(target)) {
if (target && (await isRegistryNotFound(target))) {
return exit();
}

const sources = target ? { [target]: registries[target] } : registries;

const results = await Promise.all(Object.keys(sources).map(async name => {
const { registry } = sources[name];
const start = Date.now();
let status = false;
let isTimeout = false;
try {
const response = await fetch(registry + 'nrm', { signal: AbortSignal.timeout(timeout) });
status = response.ok;
} catch (error) {
isTimeout = error.name === 'TimeoutError';
}
return {
name,
registry,
success: status,
time: Date.now() - start,
isTimeout
};
}));

const [fastest] = results.filter(each => each.success).map(each => each.time).sort((a, b) => a - b);
const results = await Promise.all(
Object.keys(sources).map(async (name) => {
const { registry } = sources[name];
const start = Date.now();
let status = false;
let isTimeout = false;
try {
const response = await fetch(registry + 'nrm', { signal: AbortSignal.timeout(timeout) });
status = response.ok;
} catch (error) {
isTimeout = error.name === 'TimeoutError';
}
return {
name,
registry,
success: status,
time: Date.now() - start,
isTimeout,
};
}),
);

const [fastest] = results
.filter((each) => each.success)
.map((each) => each.time)
.sort((a, b) => a - b);

const messages = [];
const currentRegistry = await getCurrentRegistry();
const errorMsg = chalk.red(' (Fetch error, if this is your private registry, please ignore)');
const timeoutMsg = chalk.yellow(` (Fetch timeout over ${timeout} ms)`);
const length = Math.max(...Object.keys(sources).map(key => key.length)) + 3;
const length = Math.max(...Object.keys(sources).map((key) => key.length)) + 3;
results.forEach(({ registry, success, time, name, isTimeout }) => {
const isFastest = time === fastest;
const prefix = registry === currentRegistry ? chalk.green('* ') : ' ';
let suffix = (isFastest && !target) ? chalk.bgGreenBright(time + ' ms') : isTimeout ? 'timeout' : `${time} ms`;
let suffix = isFastest && !target ? chalk.bgGreenBright(time + ' ms') : isTimeout ? 'timeout' : `${time} ms`;
if (!success) {
suffix += isTimeout ? timeoutMsg : errorMsg;
}
Expand Down
36 changes: 8 additions & 28 deletions cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,19 @@ const actions = require('./actions');
const PKG = require('./package.json');
const { program } = require('commander');

program
.version(PKG.version);
program.version(PKG.version);

program
.command('ls')
.description('List all the registries')
.action(actions.onList);
program.command('ls').description('List all the registries').action(actions.onList);

program
.command('current')
.option('-u, --show-url', 'Show the registry URL instead of the name')
.description('Show current registry name or URL')
.action(actions.onCurrent);

program
.command('use <name>')
.description('Change current registry')
.action(actions.onUse);
program.command('use <name>').description('Change current registry').action(actions.onUse);

program
.command('add <name> <url> [home]')
.description('Add custom registry')
.action(actions.onAdd);
program.command('add <name> <url> [home]').description('Add custom registry').action(actions.onAdd);

program
.command('login <name> [base64]')
Expand All @@ -47,10 +37,7 @@ program
.description('Associating a scope with a registry')
.action(actions.onSetScope);

program
.command('del-scope <scopeName>')
.description('Remove a scope')
.action(actions.onDeleteScope);
program.command('del-scope <scopeName>').description('Remove a scope').action(actions.onDeleteScope);

program
.command('set <name>')
Expand All @@ -59,15 +46,9 @@ program
.description('Set a custom registry attribute')
.action(actions.onSetAttribute);

program
.command('rename <name> <newName>')
.description('Change custom registry name')
.action(actions.onRename);
program.command('rename <name> <newName>').description('Change custom registry name').action(actions.onRename);

program
.command('del <name>')
.description('Delete custom registry')
.action(actions.onDelete);
program.command('del <name>').description('Delete custom registry').action(actions.onDelete);

program
.command('home <name> [browser]')
Expand All @@ -79,8 +60,7 @@ program
.description('Show response time for specific or all registries')
.action(actions.onTest);

program
.parse(process.argv);
program.parse(process.argv);

if (process.argv.length === 2) {
program.outputHelp();
Expand Down
4 changes: 2 additions & 2 deletions constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ const REGISTRY = 'registry';
const REPOSITORY = 'repository';
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 NRMRC = path.join(process.env[process.platform === 'win32' ? 'USERPROFILE' : 'HOME'], '.nrmrc');
const NPMRC = path.join(process.env[process.platform === 'win32' ? 'USERPROFILE' : 'HOME'], '.npmrc');

module.exports = {
NRMRC,
Expand Down
4 changes: 2 additions & 2 deletions helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const process = require('./process');
const { NRMRC, NPMRC, REGISTRY, REGISTRIES } = require('./constants');

async function readFile(file) {
return new Promise(resolve => {
return new Promise((resolve) => {
if (!fs.existsSync(file)) {
resolve({});
} else {
Expand All @@ -21,7 +21,7 @@ async function readFile(file) {
}

async function writeFile(path, content) {
return new Promise(resolve => {
return new Promise((resolve) => {
try {
fs.writeFileSync(path, ini.stringify(content));
resolve();
Expand Down
2 changes: 1 addition & 1 deletion jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ module.exports = {
globals: {
__REGISTRY__: '',
__NRM_VERSION__: null,
}
},
};
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
"star": "npm star nrm",
"test": "jest",
"changeset:version": "changeset version",
"changeset:publish": "changeset publish"
"changeset:publish": "changeset publish",
"format": "prettier . --write"
},
"keywords": [
"npm",
Expand All @@ -35,6 +36,7 @@
"devDependencies": {
"@changesets/cli": "^2.27.8",
"coffee": "^5.4.0",
"jest": "^27.4.0"
"jest": "^27.4.0",
"prettier": "^3.3.3"
}
}
5 changes: 5 additions & 0 deletions prettier.config.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module.exports = {
printWidth: 120,
singleQuote: true,
semi: true,
};
Loading
Loading