Skip to content

Commit

Permalink
feat: add flag to change the name. (#742)
Browse files Browse the repository at this point in the history
Currently defaults to the name in the package.json
  • Loading branch information
lholmquist committed Apr 26, 2024
1 parent d08e8e4 commit 2e8385f
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 6 deletions.
2 changes: 2 additions & 0 deletions bin/nodeshift
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,8 @@ function createOptions (argv) {
options.kube = 'minikube';
}

options.name = argv.name

options.projectLocation = argv.projectLocation;
options.dockerImage = argv.dockerImage;
options.imageStream = argv.imageStream;
Expand Down
19 changes: 13 additions & 6 deletions lib/config/nodeshift-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -147,13 +147,20 @@ async function setup (options = {}) {

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

// Check that the package.json has a name field first
if (!projectPackage.name) {
throw new Error('could not find required field "name" in package.json');
}
if (options.name) {
// Specified a name to use instead of the package.json name field
projectPackage.name = options.name;
logger.info(`package.json name overridden: using name ${projectPackage.name}`);
} else {
// No Flag, use the package.json name instead
// Check that the package.json has a name field first
if (!projectPackage.name) {
throw new Error('could not find required field "name" in package.json');
}

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 -.');
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 -.');
}
}

// If they are using a @scoped package like "@org/package", we need to strip off the @ symbol and convert it to
Expand Down
45 changes: 45 additions & 0 deletions test/config-tests/nodeshift-config-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1191,3 +1191,48 @@ test('nodeshift-config options username, pasword, token, apiServer, insecure(fal
t.end();
});
});

test('nodeshift-config options - differernt name than package.json', (t) => {
const nodeshiftConfig = proxyquire('../../lib/config/nodeshift-config', {
'./login-config': {
doNodeshiftLogin: () => {
t.fail('This should not be called');
},
doNodeshiftLogout: () => {
t.fail('This should not be called');
return Promise.resolve();
},
checkForNodeshiftLogin: () => {
t.pass();
return Promise.resolve();
}
},
'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 options = {
name: 'funTimes'
};

const { name: currentName } = require('../../package.json');
nodeshiftConfig(options).then((config) => {
t.notEqual(config.name, currentName, 'should not be the project name');
t.end();
});
});

0 comments on commit 2e8385f

Please sign in to comment.