Skip to content

Commit

Permalink
feat(js): add scopes option for verdaccio (#26918)
Browse files Browse the repository at this point in the history
  • Loading branch information
xiongemi authored Jul 24, 2024
1 parent 8afd3ea commit 28cd0c4
Show file tree
Hide file tree
Showing 4 changed files with 127 additions and 68 deletions.
5 changes: 5 additions & 0 deletions docs/generated/packages/js/executors/verdaccio.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@
"type": "boolean",
"description": "Clear local registry storage before starting Verdaccio",
"default": true
},
"scopes": {
"type": "array",
"description": "Scopes to be added to the Verdaccio config",
"items": { "type": "string" }
}
},
"required": ["port"],
Expand Down
1 change: 1 addition & 0 deletions packages/js/src/executors/verdaccio/schema.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ export interface VerdaccioExecutorSchema {
port?: number;
config?: string;
clear?: boolean;
scopes?: string[];
}
7 changes: 7 additions & 0 deletions packages/js/src/executors/verdaccio/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,13 @@
"type": "boolean",
"description": "Clear local registry storage before starting Verdaccio",
"default": true
},
"scopes": {
"type": "array",
"description": "Scopes to be added to the Verdaccio config",
"items": {
"type": "string"
}
}
},
"required": ["port"]
Expand Down
182 changes: 114 additions & 68 deletions packages/js/src/executors/verdaccio/verdaccio.impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,59 +138,88 @@ function setupNpm(options: VerdaccioExecutorSchema) {
return () => {};
}

let npmRegistryPath: string;
let npmRegistryPaths: string[] = [];
const scopes: string[] = ['', ...(options.scopes || [])];

try {
npmRegistryPath = execSync(
`npm config get registry --location ${options.location}`,
{ env }
)
?.toString()
?.trim()
?.replace('\u001b[2K\u001b[1G', ''); // strip out ansi codes
execSync(
`npm config set registry http://localhost:${options.port}/ --location ${options.location}`,
{ env }
);
execSync(
`npm config set //localhost:${options.port}/:_authToken="secretVerdaccioToken" --location ${options.location}`,
{ env }
);
logger.info(`Set npm registry to http://localhost:${options.port}/`);
} catch (e) {
throw new Error(
`Failed to set npm registry to http://localhost:${options.port}/: ${e.message}`
);
}
scopes.forEach((scope) => {
const scopeName = scope ? `${scope}:` : '';
try {
npmRegistryPaths.push(
execSync(
`npm config get '${scopeName}registry' --location ${options.location}`,
{ env }
)
?.toString()
?.trim()
?.replace('\u001b[2K\u001b[1G', '') // strip out ansi codes
);
execSync(
`npm config set '${scopeName}registry' http://localhost:${options.port}/ --location ${options.location}`,
{ env }
);

return () => {
try {
const currentNpmRegistryPath = execSync(
`npm config get registry --location ${options.location}`,
{ env }
)
?.toString()
?.trim()
?.replace('\u001b[2K\u001b[1G', ''); // strip out ansi codes
if (npmRegistryPath && currentNpmRegistryPath.includes('localhost')) {
execSync(
`npm config set registry ${npmRegistryPath} --location ${options.location}`,
`npm config set //localhost:${options.port}/:_authToken="secretVerdaccioToken" --location ${options.location}`,
{ env }
);
logger.info(`Reset npm registry to ${npmRegistryPath}`);
} else {
execSync(`npm config delete registry --location ${options.location}`, {
env,
});
logger.info('Cleared custom npm registry');

logger.info(
`Set npm ${scopeName}registry to http://localhost:${options.port}/`
);
} catch (e) {
throw new Error(
`Failed to set npm ${scopeName}registry to http://localhost:${options.port}/: ${e.message}`
);
}
execSync(
`npm config delete //localhost:${options.port}/:_authToken --location ${options.location}`,
{ env }
);
} catch (e) {
throw new Error(`Failed to reset npm registry: ${e.message}`);
}
};
});

return () => {
try {
const currentNpmRegistryPath = execSync(
`npm config get registry --location ${options.location}`,
{ env }
)
?.toString()
?.trim()
?.replace('\u001b[2K\u001b[1G', ''); // strip out ansi codes
if (
npmRegistryPaths.length > 0 &&
currentNpmRegistryPath.includes('localhost')
) {
scopes.forEach((scope, index) => {
const scopeName = scope ? `${scope}:` : '';

execSync(
`npm config set '${scopeName}registry' ${npmRegistryPaths[index]} --location ${options.location}`,
{ env }
);
logger.info(
`Reset npm ${scopeName}registry to ${npmRegistryPaths[index]}`
);
});
} else {
execSync(
`npm config delete registry --location ${options.location}`,
{
env,
}
);
logger.info('Cleared custom npm registry');
}
execSync(
`npm config delete //localhost:${options.port}/:_authToken --location ${options.location}`,
{ env }
);
} catch (e) {
throw new Error(`Failed to reset npm registry: ${e.message}`);
}
};
} catch (e) {
throw new Error(
`Failed to set npm registry to http://localhost:${options.port}/: ${e.message}`
);
}
}

function getYarnUnsafeHttpWhitelist(isYarnV1: boolean) {
Expand Down Expand Up @@ -227,6 +256,8 @@ function setYarnUnsafeHttpWhitelist(

function setupYarn(options: VerdaccioExecutorSchema) {
let isYarnV1;
let yarnRegistryPaths: string[] = [];
const scopes: string[] = ['', ...(options.scopes || [])];

try {
isYarnV1 =
Expand All @@ -238,20 +269,28 @@ function setupYarn(options: VerdaccioExecutorSchema) {
try {
const registryConfigName = isYarnV1 ? 'registry' : 'npmRegistryServer';

const yarnRegistryPath = execSync(`yarn config get ${registryConfigName}`, {
env,
})
?.toString()
?.trim()
?.replace('\u001b[2K\u001b[1G', ''); // strip out ansi codes
scopes.forEach((scope) => {
const scopeName = scope ? `${scope}:` : '';

execSync(
`yarn config set ${registryConfigName} http://localhost:${options.port}/` +
(options.location === 'user' ? ' --home' : ''),
{ env }
);
yarnRegistryPaths.push(
execSync(`yarn config get ${scopeName}${registryConfigName}`, {
env,
})
?.toString()
?.trim()
?.replace('\u001b[2K\u001b[1G', '') // strip out ansi codes
);

logger.info(`Set yarn registry to http://localhost:${options.port}/`);
execSync(
`yarn config set ${scopeName}${registryConfigName} http://localhost:${options.port}/` +
(options.location === 'user' ? ' --home' : ''),
{ env }
);

logger.info(
`Set yarn ${scopeName}registry to http://localhost:${options.port}/`
);
});

const currentWhitelist: Set<string> | null =
getYarnUnsafeHttpWhitelist(isYarnV1);
Expand All @@ -277,15 +316,22 @@ function setupYarn(options: VerdaccioExecutorSchema) {
?.toString()
?.trim()
?.replace('\u001b[2K\u001b[1G', ''); // strip out ansi codes
if (yarnRegistryPath && currentYarnRegistryPath.includes('localhost')) {
execSync(
`yarn config set ${registryConfigName} ${yarnRegistryPath}` +
(options.location === 'user' ? ' --home' : ''),
{ env }
);
logger.info(
`Reset yarn ${registryConfigName} to ${yarnRegistryPath}`
);
if (
yarnRegistryPaths.length > 0 &&
currentYarnRegistryPath.includes('localhost')
) {
scopes.forEach((scope, index) => {
const scopeName = scope ? `${scope}:` : '';

execSync(
`yarn config set ${scopeName}${registryConfigName} ${yarnRegistryPaths[index]}` +
(options.location === 'user' ? ' --home' : ''),
{ env }
);
logger.info(
`Reset yarn ${scopeName}${registryConfigName} to ${yarnRegistryPaths[index]}`
);
});
} else {
execSync(
`yarn config ${
Expand Down

0 comments on commit 28cd0c4

Please sign in to comment.