@sasjs/cli 6.4.6 → 6.4.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -353,7 +353,7 @@ function printHelpText() {
353
353
  { name: 'help', aliases: ['h'] },
354
354
  { name: 'request', aliases: ['rq'] },
355
355
  { name: 'run', aliases: ['r'] },
356
- { name: 'version', aliases: ['v'] },
356
+ { name: 'version', aliases: ['v', '-v', '--version'] },
357
357
  { name: 'web', aliases: ['w'] }
358
358
  ];
359
359
  outputCommands = "".concat(commands
@@ -35,17 +35,63 @@ var __generator = (this && this.__generator) || function (thisArg, body) {
35
35
  if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
36
36
  }
37
37
  };
38
+ var __importDefault = (this && this.__importDefault) || function (mod) {
39
+ return (mod && mod.__esModule) ? mod : { "default": mod };
40
+ };
38
41
  Object.defineProperty(exports, "__esModule", { value: true });
42
+ var fs_1 = require("fs");
43
+ var os_1 = require("os");
44
+ var path_1 = __importDefault(require("path"));
39
45
  var version_1 = require("../version");
46
+ var writePackage = function (dir, version) {
47
+ (0, fs_1.mkdirSync)(dir, { recursive: true });
48
+ (0, fs_1.writeFileSync)(path_1.default.join(dir, 'package.json'), JSON.stringify({ version: version }));
49
+ };
50
+ var tempRoot = function () { return (0, fs_1.mkdtempSync)(path_1.default.join((0, os_1.tmpdir)(), 'sasjs-version-')); };
51
+ var packageRoot = function () { return path_1.default.join(tempRoot(), 'node_modules', '@sasjs', 'cli'); };
40
52
  describe('printVersion', function () {
41
- it('should return sasjs version', function () { return __awaiter(void 0, void 0, void 0, function () {
53
+ it('reports the package that is running, with no global install involved', function () { return __awaiter(void 0, void 0, void 0, function () {
54
+ var message;
42
55
  return __generator(this, function (_a) {
43
56
  switch (_a.label) {
44
- case 0: return [4 /*yield*/, expect((0, version_1.printVersion)()).toResolve()];
57
+ case 0: return [4 /*yield*/, (0, version_1.printVersion)()];
45
58
  case 1:
46
- _a.sent();
59
+ message = _a.sent();
60
+ expect(message).toContain(path_1.default.resolve(__dirname, '../../../..'));
61
+ expect(message).toMatch(/^You are using (SASjs CLI v|a linked version)/);
47
62
  return [2 /*return*/];
48
63
  }
49
64
  });
50
65
  }); });
51
66
  });
67
+ describe('versionMessage', function () {
68
+ it('reports an installed version', function () {
69
+ var root = packageRoot();
70
+ writePackage(root, '6.4.6');
71
+ expect((0, version_1.versionMessage)(root)).toEqual('You are using SASjs CLI v6.4.6');
72
+ });
73
+ it('reports a source checkout as a linked version', function () {
74
+ var root = path_1.default.join(tempRoot(), 'cli');
75
+ writePackage(root, '6.4.7-alpha.1');
76
+ expect((0, version_1.versionMessage)(root)).toEqual("You are using a linked version of SASjs CLI (v6.4.7-alpha.1) running from sources at ".concat(root));
77
+ });
78
+ it('leaves the version out of a source checkout that has none', function () {
79
+ var root = path_1.default.join(tempRoot(), 'cli');
80
+ writePackage(root, undefined);
81
+ expect((0, version_1.versionMessage)(root)).toEqual("You are using a linked version of SASjs CLI running from sources at ".concat(root));
82
+ });
83
+ it('reports the whole version string, not a fragment of it', function () {
84
+ var root = packageRoot();
85
+ writePackage(root, '1.2.3-beta.4');
86
+ expect((0, version_1.versionMessage)(root)).toEqual('You are using SASjs CLI v1.2.3-beta.4');
87
+ });
88
+ it('throws an actionable error when the package.json is missing', function () {
89
+ var root = packageRoot();
90
+ expect(function () { return (0, version_1.versionMessage)(root); }).toThrow(/Unable to read the CLI version from/);
91
+ });
92
+ it('throws when an installed package carries no version', function () {
93
+ var root = packageRoot();
94
+ writePackage(root, undefined);
95
+ expect(function () { return (0, version_1.versionMessage)(root); }).toThrow(/has no version in its package.json/);
96
+ });
97
+ });
@@ -1 +1,14 @@
1
+ /**
2
+ * The message `sasjs version` prints for a given package root.
3
+ *
4
+ * Reads the version from the package.json of the package that is running, so
5
+ * it works for every install shape - global, project-local, `npx`, a temp
6
+ * prefix, or embedded as a dependency. A package root outside a
7
+ * `node_modules` folder is a source checkout (an `npm link`, or a local
8
+ * build), and is reported as such; a source checkout carries no version until
9
+ * semantic-release writes one, so the version is left out when it is absent.
10
+ *
11
+ * @param packageRoot - the folder holding the package.json of the running CLI.
12
+ */
13
+ export declare const versionMessage: (packageRoot: string) => string;
1
14
  export declare function printVersion(): Promise<string>;
@@ -35,22 +35,55 @@ var __generator = (this && this.__generator) || function (thisArg, body) {
35
35
  if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
36
36
  }
37
37
  };
38
+ var __importDefault = (this && this.__importDefault) || function (mod) {
39
+ return (mod && mod.__esModule) ? mod : { "default": mod };
40
+ };
38
41
  Object.defineProperty(exports, "__esModule", { value: true });
42
+ exports.versionMessage = void 0;
39
43
  exports.printVersion = printVersion;
40
- var shell_1 = require("../../utils/shell");
44
+ var fs_1 = require("fs");
45
+ var path_1 = __importDefault(require("path"));
46
+ /**
47
+ * The message `sasjs version` prints for a given package root.
48
+ *
49
+ * Reads the version from the package.json of the package that is running, so
50
+ * it works for every install shape - global, project-local, `npx`, a temp
51
+ * prefix, or embedded as a dependency. A package root outside a
52
+ * `node_modules` folder is a source checkout (an `npm link`, or a local
53
+ * build), and is reported as such; a source checkout carries no version until
54
+ * semantic-release writes one, so the version is left out when it is absent.
55
+ *
56
+ * @param packageRoot - the folder holding the package.json of the running CLI.
57
+ */
58
+ var versionMessage = function (packageRoot) {
59
+ var version;
60
+ try {
61
+ version = JSON.parse((0, fs_1.readFileSync)(path_1.default.join(packageRoot, 'package.json'), 'utf8')).version;
62
+ }
63
+ catch (err) {
64
+ throw new Error("Unable to read the CLI version from ".concat(packageRoot, ": ").concat(err, ". ") +
65
+ "Reinstall the CLI - 'npm i -g @sasjs/cli@latest'.");
66
+ }
67
+ var fromSources = !packageRoot.split(path_1.default.sep).includes('node_modules');
68
+ if (fromSources) {
69
+ return version
70
+ ? "You are using a linked version of SASjs CLI (v".concat(version, ") running from sources at ").concat(packageRoot)
71
+ : "You are using a linked version of SASjs CLI running from sources at ".concat(packageRoot);
72
+ }
73
+ if (!version) {
74
+ throw new Error("The installed CLI at ".concat(packageRoot, " has no version in its package.json. ") +
75
+ "Reinstall it - 'npm i -g @sasjs/cli@latest'.");
76
+ }
77
+ return "You are using SASjs CLI v".concat(version);
78
+ };
79
+ exports.versionMessage = versionMessage;
41
80
  function printVersion() {
42
81
  return __awaiter(this, void 0, void 0, function () {
43
- var stdout, line, version, message;
82
+ var packageRoot, message;
44
83
  var _a;
45
84
  return __generator(this, function (_b) {
46
- stdout = (0, shell_1.exec)("npm list -g @sasjs/cli", { silent: true }).stdout;
47
- line = stdout.split('\n').find(function (l) { return l.includes('@sasjs/cli'); }) || '';
48
- version = line.split('@')[2].trim();
49
- message = version.includes('->')
50
- ? "You are using a linked version of SASjs CLI running from sources at ".concat(version
51
- .replace('->', '')
52
- .trim())
53
- : "You are using SASjs CLI v".concat(version);
85
+ packageRoot = path_1.default.resolve(__dirname, '../../..');
86
+ message = (0, exports.versionMessage)(packageRoot);
54
87
  (_a = process.logger) === null || _a === void 0 ? void 0 : _a.info(message);
55
88
  return [2 /*return*/, message];
56
89
  });
@@ -12,7 +12,7 @@ exports.aliasMap = new Map([
12
12
  ['help', ['h']],
13
13
  ['request', ['rq']],
14
14
  ['run', ['r']],
15
- ['version', ['v']],
15
+ ['version', ['v', '-v', '--version']],
16
16
  ['web', ['w']]
17
17
  ]);
18
18
  var getAllSupportedAliases = function () { return exports.aliasMap.keys(); };
@@ -1,2 +1,8 @@
1
1
  import { CommandBase } from './commandBase';
2
+ /**
3
+ * Rewrites a version flag in the command position to the `version` command.
4
+ *
5
+ * @param args - the process arguments, as `[node, sasjs, ...]`.
6
+ */
7
+ export declare const normaliseArgs: (args: string[]) => string[];
2
8
  export declare const parse: (args: string[]) => CommandBase;
@@ -1,15 +1,57 @@
1
1
  "use strict";
2
+ var __read = (this && this.__read) || function (o, n) {
3
+ var m = typeof Symbol === "function" && o[Symbol.iterator];
4
+ if (!m) return o;
5
+ var i = m.call(o), r, ar = [], e;
6
+ try {
7
+ while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
8
+ }
9
+ catch (error) { e = { error: error }; }
10
+ finally {
11
+ try {
12
+ if (r && !r.done && (m = i["return"])) m.call(i);
13
+ }
14
+ finally { if (e) throw e.error; }
15
+ }
16
+ return ar;
17
+ };
18
+ var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
19
+ if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
20
+ if (ar || !(i in from)) {
21
+ if (!ar) ar = Array.prototype.slice.call(from, 0, i);
22
+ ar[i] = from[i];
23
+ }
24
+ }
25
+ return to.concat(ar || Array.prototype.slice.call(from));
26
+ };
2
27
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.parse = void 0;
28
+ exports.parse = exports.normaliseArgs = void 0;
4
29
  var getBaseCommand_1 = require("../../utils/getBaseCommand");
5
30
  var commandFactory_1 = require("./commandFactory");
31
+ // `sasjs version`, `sasjs v`, `sasjs -v` and `sasjs --version` all mean the
32
+ // same thing. yargs reads a leading `-v`/`--version` as an option rather than
33
+ // a command, and its own version handling is switched off, so the token is
34
+ // rewritten to the command name before dispatch.
35
+ var versionFlags = ['-v', '--version'];
36
+ /**
37
+ * Rewrites a version flag in the command position to the `version` command.
38
+ *
39
+ * @param args - the process arguments, as `[node, sasjs, ...]`.
40
+ */
41
+ var normaliseArgs = function (args) {
42
+ var _a = __read(args, 3), command = _a[2];
43
+ return versionFlags.includes(command)
44
+ ? __spreadArray(__spreadArray(__spreadArray([], __read(args.slice(0, 2)), false), ['version'], false), __read(args.slice(3)), false) : args;
45
+ };
46
+ exports.normaliseArgs = normaliseArgs;
6
47
  var parse = function (args) {
7
48
  var _a;
8
- var baseCommand = (0, getBaseCommand_1.getBaseCommand)(args);
49
+ var commandArgs = (0, exports.normaliseArgs)(args);
50
+ var baseCommand = (0, getBaseCommand_1.getBaseCommand)(commandArgs);
9
51
  if (!Array.from(commandFactory_1.commandFactory.keys()).includes(baseCommand.name)) {
10
52
  (_a = process.logger) === null || _a === void 0 ? void 0 : _a.error('Invalid SASjs command! Run `sasjs help` for a full list of available commands.');
11
53
  process.exit(1);
12
54
  }
13
- return commandFactory_1.commandFactory.get(baseCommand.name)(args);
55
+ return commandFactory_1.commandFactory.get(baseCommand.name)(commandArgs);
14
56
  };
15
57
  exports.parse = parse;
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,40 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ var versionCommand_1 = require("../../../commands/version/versionCommand");
4
+ var parse_1 = require("../parse");
5
+ describe('parse', function () {
6
+ it.each(['version', 'v', '-v', '--version'])('resolves `sasjs %s` to the version command', function (token) {
7
+ var command = (0, parse_1.parse)(['node', 'sasjs', token]);
8
+ expect(command).toBeInstanceOf(versionCommand_1.VersionCommand);
9
+ expect(command.name).toEqual('version');
10
+ });
11
+ it('leaves another command as it is', function () {
12
+ var command = (0, parse_1.parse)(['node', 'sasjs', 'help']);
13
+ expect(command.name).toEqual('help');
14
+ });
15
+ });
16
+ describe('normaliseArgs', function () {
17
+ it.each(['-v', '--version'])('rewrites %s in the command position', function (flag) {
18
+ expect((0, parse_1.normaliseArgs)(['node', 'sasjs', flag])).toEqual([
19
+ 'node',
20
+ 'sasjs',
21
+ 'version'
22
+ ]);
23
+ });
24
+ it('keeps the arguments that follow the flag', function () {
25
+ expect((0, parse_1.normaliseArgs)(['node', 'sasjs', '--version', 'extra'])).toEqual([
26
+ 'node',
27
+ 'sasjs',
28
+ 'version',
29
+ 'extra'
30
+ ]);
31
+ });
32
+ it('does not rewrite a flag that is not in the command position', function () {
33
+ var args = ['node', 'sasjs', 'run', 'program.sas', '-v'];
34
+ expect((0, parse_1.normaliseArgs)(args)).toEqual(args);
35
+ });
36
+ it('leaves the command name alone', function () {
37
+ var args = ['node', 'sasjs', 'version'];
38
+ expect((0, parse_1.normaliseArgs)(args)).toEqual(args);
39
+ });
40
+ });
package/package.json CHANGED
@@ -123,5 +123,5 @@
123
123
  "ws": "8.21.0",
124
124
  "nanoid": "3.3.18"
125
125
  },
126
- "version": "6.4.6"
126
+ "version": "6.4.7"
127
127
  }