Skip to content

Commit

Permalink
fix(config): Allow scoped applications in the package name. (#539)
Browse files Browse the repository at this point in the history
* fix(config): Allow scoped applications in the package name.

* This modifies the logic that was used to determine if an applications package name was valid.  Previously, nodeshift would error when using scoped packages.  Since @org/pkg is valid in npm,  this is now valid with nodeshift.  scoped package names will be updated to org-pkg for the Openshift Objects the CLI creates.

fixes #538
  • Loading branch information
lholmquist committed Feb 12, 2021
1 parent 4206d3b commit 0e4a5db
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 4 deletions.
12 changes: 8 additions & 4 deletions lib/config/nodeshift-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,11 +127,15 @@ async function setup (options = {}) {

logger.info(`using namespace ${config.namespace.name} at ${kubeConfig.getCurrentCluster().server}`);

if (!projectPackage.name.match(/^[a-z][0-9a-z-]+[0-9a-z]$/)) {
if (!projectPackage.name.match(/^(@[a-z0-9-~][a-z0-9-._~]*\/)?[a-z0-9-~][a-z0-9-._~]*$/)) {
throw new Error('"name" in package.json can only consist lower-case letters, numbers, and dashes. It must start with a letter and can\'t end with a -.');
}

options.outputImageStreamName = options.outputImageStreamName || projectPackage.name;
// If they are using a @scoped package like "@org/package", we need to strip off the @ symbol and convert it to
// something like "org-package"
const projectName = projectPackage.name.replace('@', '').replace('/', '-');

options.outputImageStreamName = options.outputImageStreamName || projectName;
options.outputImageStreamTag = options.outputImageStreamTag || 'latest';

// TODO: do build strategy here
Expand All @@ -155,10 +159,10 @@ async function setup (options = {}) {
// We don't want to hard code the port later, so add it here for later use in the application descriptors
// Make sure it is a number
port: options.deploy && options.deploy.port ? parseInt(options.deploy.port, 10) : 8080,
projectName: projectPackage.name,
projectName: projectName,
projectVersion: projectPackage.version || '0.0.0',
// Since we are only doing s2i builds(atm), append the s2i bit to the end
buildName: `${projectPackage.name}-s2i`, // TODO(lholmquist): this should probably change?
buildName: `${projectName}-s2i`, // TODO(lholmquist): this should probably change?
// Load an instance of the Openshift Rest Client, https://www.npmjs.com/package/openshift-rest-client
openshiftRestClient: restClient,
dockerClient
Expand Down
60 changes: 60 additions & 0 deletions test/config-tests/nodeshift-config-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,66 @@ test('nodeshift-config no package.json', (t) => {
});
});

test('nodeshift-config valid "@scope name" in package.json', (t) => {
const nodeshiftConfig = proxyquire('../../lib/config/nodeshift-config', {
'openshift-rest-client': {
OpenshiftClient: () => {
return Promise.resolve({
kubeconfig: {
getCurrentContext: () => {
return 'nodey/ip/other';
},
getCurrentCluster: () => {
return { server: 'http://mock-cluster' };
},
getContexts: () => {
return [{ name: 'nodey/ip/other', namespace: 'test-namespace' }];
}
}
});
}
}
});

const tmpDir = require('os').tmpdir();
const join = require('path').join;
const fs = require('fs');

const options = {
projectLocation: join(tmpDir, 'nodeshift-valid-scope-package-name-test')
};

if (!fs.existsSync(options.projectLocation)) {
fs.mkdirSync(options.projectLocation);
}

// Create a temp package that has an invalid name, but extends the example JSON
fs.writeFileSync(
join(options.projectLocation, 'package.json'),
JSON.stringify(
Object.assign(
{},
require('../../examples/sample-project/package.json'),
{
name: '@org/pkg'
}
)
)
);

nodeshiftConfig(options)
.then(config => {
t.equal(config.projectName, 'org-pkg', 'modified name');
t.equal(config.projectPackage.name, '@org/pkg', 'not modified');
t.pass('Valid Scoped Name');
t.end();
})
.catch((err) => {
t.fail(err);
t.end();
});
});

test('nodeshift-config invalid "name" in package.json', (t) => {
const nodeshiftConfig = proxyquire('../../lib/config/nodeshift-config', {
'openshift-rest-client': {
Expand Down

0 comments on commit 0e4a5db

Please sign in to comment.