sn-typescript-util 1.1.1 → 1.2.1

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.
package/bin/snts.js CHANGED
@@ -1,16 +1,14 @@
1
1
  #!/usr/bin/env node
2
- const childProcess = require('child_process');
3
- const fs = require('fs');
4
- const path = require('path');
5
- const util = require('util');
6
- const exec = util.promisify(childProcess.exec);
7
- const { description, version } = require('./../package.json');
8
- const { program } = require('commander');
9
- const { bold, cyan, red } = require('colorette');
10
- const { cancel, intro, outro, spinner } = require('@clack/prompts');
2
+ import { Command } from 'commander';
3
+ import { execFile } from 'node:child_process';
4
+ import path from 'path';
5
+ import { readFileSync } from 'fs';
6
+ import { fileURLToPath } from 'url';
7
+ import { bold, red } from 'colorette';
8
+ import { intro, outro, spinner } from '@clack/prompts';
11
9
  async function doBuild() {
12
10
  const s = startPrompts('Installing configs', 'Build started');
13
- return await exec(getFilePath('init.rb'), (stdout) => {
11
+ return await execFile(getFilePath('init.rb'), (stdout) => {
14
12
  stopPrompt(s, 'Configs installed');
15
13
  runSync();
16
14
  return stdout;
@@ -18,14 +16,14 @@ async function doBuild() {
18
16
  }
19
17
  async function doCompile() {
20
18
  const s = startPrompts('Processing', 'Compile started');
21
- return await exec(getFilePath('compile.rb'), (stdout) => {
19
+ return await execFile(getFilePath('compile.rb'), (stdout) => {
22
20
  stopPrompt(s, 'Completed');
23
21
  return stdout;
24
22
  });
25
23
  }
26
24
  async function doSync() {
27
25
  const s = startPrompts('Processing', 'Sync started');
28
- return await exec(getFilePath('sync.sh'), (stdout) => {
26
+ return await execFile(getFilePath('sync.sh'), (stdout) => {
29
27
  stopPrompt(s, 'Completed');
30
28
  return stdout;
31
29
  });
@@ -33,7 +31,7 @@ async function doSync() {
33
31
  function getBuildName() {
34
32
  const defaultBuild = 'utah';
35
33
  try {
36
- const workspace = JSON.parse(getWorkspaceConfig());
34
+ const workspace = getWorkspaceFile();
37
35
  const app = workspace.ACTIVE_APPLICATION;
38
36
  const build = workspace.ALL_APPLICATIONS[app].BUILD_NAME;
39
37
  return Object.entries(build).length !== 0
@@ -49,11 +47,14 @@ function getErrorMsg() {
49
47
  var msg = `No active application detected. Please create a project with the ServiceNow Extension for VS Code.\n\n${url}`;
50
48
  return console.error(bold(red(msg)));
51
49
  }
52
- function getFilePath(file) {
53
- return `${path.join(__dirname, '../scripts')}/${file}`;
50
+ function getFilePath(file, dir = 'scripts') {
51
+ const __filename = fileURLToPath(import.meta.url);
52
+ const __dirname = path.dirname(__filename);
53
+ return `${path.join(__dirname, `../${dir}`)}/${file}`;
54
54
  }
55
- function getOption(opts) {
56
- const option = Object.keys(opts).toString();
55
+ function getOption(program) {
56
+ program.parse(process.argv).opts();
57
+ const option = Object.keys(program.opts()).toString();
57
58
  const options = {
58
59
  build: () => {
59
60
  doBuild();
@@ -70,44 +71,49 @@ function getOption(opts) {
70
71
  };
71
72
  return (options[option] || options['default'])();
72
73
  }
73
- function getWorkspaceConfig() {
74
- return fs.readFileSync('./system/sn-workspace.json');
74
+ async function getPackageInfo() {
75
+ return JSON.parse(readFileSync(getFilePath('package.json', '.')).toString());
76
+ }
77
+ function getWorkspaceFile() {
78
+ return JSON.parse(readFileSync('./system/sn-workspace.json').toString())
79
+ .ACTIVE_APPLICATION;
75
80
  }
76
- function hasApplication() {
81
+ async function hasApplication() {
77
82
  try {
78
- const app = JSON.parse(getWorkspaceConfig()).ACTIVE_APPLICATION;
79
- return Object.entries(app).length === 0 ? getErrorMsg() : true;
83
+ const workspace = await getWorkspaceFile();
84
+ return Object.entries(workspace).length === 0 ? getErrorMsg() : true;
80
85
  }
81
86
  catch (e) {
82
87
  getErrorMsg();
83
88
  return process.exit(1);
84
89
  }
85
90
  }
86
- (() => {
91
+ (async () => {
87
92
  return init();
88
93
  })();
89
- function init() {
90
- program.description(description);
91
- program.version(version);
94
+ async function init() {
95
+ const program = new Command();
96
+ const info = await getPackageInfo();
97
+ program.description(info.description);
98
+ program.version(info.version);
92
99
  program.option('-b, --build', 'build project utility files & package dependencies');
93
100
  program.option('-c, --compile', 'compile TypeScript files to JavaScript & move to src');
94
101
  program.option('-s, --sync', 'sync new instance-based src files to the ts directory');
95
- program.parse(process.argv).opts();
96
- return hasApplication() && getOption(program.opts());
102
+ return hasApplication() && getOption(program);
97
103
  }
98
104
  function introPrompt(msg) {
99
105
  return intro(msg);
100
106
  }
101
107
  async function runInstall() {
102
108
  const s = startPrompts('Installing packages', null);
103
- return await exec(getFilePath('install.sh'), (stdout) => {
109
+ return await execFile(getFilePath('install.sh'), (stdout) => {
104
110
  stopPrompt(s, 'Packages installed');
105
111
  outro('Completed');
106
112
  return stdout;
107
113
  });
108
114
  }
109
115
  async function runSync() {
110
- return await exec(getFilePath('sync.sh'), (stdout) => {
116
+ return await execFile(getFilePath('sync.sh'), (stdout) => {
111
117
  runInstall();
112
118
  return stdout;
113
119
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "sn-typescript-util",
3
- "version": "1.1.1",
3
+ "version": "1.2.1",
4
4
  "description": "A TypeScript utility for ServiceNow developers using VS Code",
5
5
  "bin": {
6
6
  "snts": "bin/snts.js"
@@ -16,6 +16,7 @@
16
16
  "type": "git",
17
17
  "url": "https://github.com/stevengregory/sn-typescript-util.git"
18
18
  },
19
+ "type": "module",
19
20
  "dependencies": {
20
21
  "@clack/prompts": "^0.6.3",
21
22
  "@types/node": "^20.4.2",
package/src/snts.ts CHANGED
@@ -1,18 +1,16 @@
1
1
  #!/usr/bin/env node
2
2
 
3
- const childProcess = require('child_process');
4
- const fs = require('fs');
5
- const path = require('path');
6
- const util = require('util');
7
- const exec = util.promisify(childProcess.exec);
8
- const { description, version } = require('./../package.json');
9
- const { program } = require('commander');
10
- const { bold, cyan, red } = require('colorette');
11
- const { cancel, intro, outro, spinner } = require('@clack/prompts');
3
+ import { Command } from 'commander';
4
+ import { execFile } from 'node:child_process';
5
+ import path from 'path';
6
+ import { readFileSync } from 'fs';
7
+ import { fileURLToPath } from 'url';
8
+ import { bold, red } from 'colorette';
9
+ import { intro, outro, spinner } from '@clack/prompts';
12
10
 
13
11
  async function doBuild() {
14
12
  const s = startPrompts('Installing configs', 'Build started');
15
- return await exec(getFilePath('init.rb'), (stdout) => {
13
+ return await execFile(getFilePath('init.rb'), (stdout) => {
16
14
  stopPrompt(s, 'Configs installed');
17
15
  runSync();
18
16
  return stdout;
@@ -21,7 +19,7 @@ async function doBuild() {
21
19
 
22
20
  async function doCompile() {
23
21
  const s = startPrompts('Processing', 'Compile started');
24
- return await exec(getFilePath('compile.rb'), (stdout) => {
22
+ return await execFile(getFilePath('compile.rb'), (stdout) => {
25
23
  stopPrompt(s, 'Completed');
26
24
  return stdout;
27
25
  });
@@ -29,18 +27,18 @@ async function doCompile() {
29
27
 
30
28
  async function doSync() {
31
29
  const s = startPrompts('Processing', 'Sync started');
32
- return await exec(getFilePath('sync.sh'), (stdout) => {
30
+ return await execFile(getFilePath('sync.sh'), (stdout: any) => {
33
31
  stopPrompt(s, 'Completed');
34
32
  return stdout;
35
33
  });
36
34
  }
37
35
 
38
36
  function getBuildName() {
39
- const defaultBuild = 'utah';
37
+ const defaultBuild: string = 'utah';
40
38
  try {
41
- const workspace = JSON.parse(getWorkspaceConfig());
42
- const app = workspace.ACTIVE_APPLICATION;
43
- const build = workspace.ALL_APPLICATIONS[app].BUILD_NAME;
39
+ const workspace = getWorkspaceFile();
40
+ const app: string = workspace.ACTIVE_APPLICATION;
41
+ const build: string = workspace.ALL_APPLICATIONS[app].BUILD_NAME;
44
42
  return Object.entries(build).length !== 0
45
43
  ? build.toLowerCase()
46
44
  : defaultBuild;
@@ -50,17 +48,20 @@ function getBuildName() {
50
48
  }
51
49
 
52
50
  function getErrorMsg() {
53
- var url = `https://docs.servicenow.com/bundle/${getBuildName()}-application-development/page/build/applications/task/create-project.html`;
54
- var msg = `No active application detected. Please create a project with the ServiceNow Extension for VS Code.\n\n${url}`;
51
+ var url: string = `https://docs.servicenow.com/bundle/${getBuildName()}-application-development/page/build/applications/task/create-project.html`;
52
+ var msg: string = `No active application detected. Please create a project with the ServiceNow Extension for VS Code.\n\n${url}`;
55
53
  return console.error(bold(red(msg)));
56
54
  }
57
55
 
58
- function getFilePath(file) {
59
- return `${path.join(__dirname, '../scripts')}/${file}`;
56
+ function getFilePath(file: string, dir: string = 'scripts') {
57
+ const __filename = fileURLToPath(import.meta.url);
58
+ const __dirname = path.dirname(__filename);
59
+ return `${path.join(__dirname, `../${dir}`)}/${file}`;
60
60
  }
61
61
 
62
- function getOption(opts) {
63
- const option = Object.keys(opts).toString();
62
+ function getOption(program: any) {
63
+ program.parse(process.argv).opts();
64
+ const option = Object.keys(program.opts()).toString();
64
65
  const options = {
65
66
  build: () => {
66
67
  doBuild();
@@ -78,27 +79,34 @@ function getOption(opts) {
78
79
  return (options[option] || options['default'])();
79
80
  }
80
81
 
81
- function getWorkspaceConfig() {
82
- return fs.readFileSync('./system/sn-workspace.json');
82
+ async function getPackageInfo() {
83
+ return JSON.parse(readFileSync(getFilePath('package.json', '.')).toString());
84
+ }
85
+
86
+ function getWorkspaceFile() {
87
+ return JSON.parse(readFileSync('./system/sn-workspace.json').toString())
88
+ .ACTIVE_APPLICATION;
83
89
  }
84
90
 
85
- function hasApplication() {
91
+ async function hasApplication() {
86
92
  try {
87
- const app = JSON.parse(getWorkspaceConfig()).ACTIVE_APPLICATION;
88
- return Object.entries(app).length === 0 ? getErrorMsg() : true;
93
+ const workspace = await getWorkspaceFile();
94
+ return Object.entries(workspace).length === 0 ? getErrorMsg() : true;
89
95
  } catch (e) {
90
96
  getErrorMsg();
91
97
  return process.exit(1);
92
98
  }
93
99
  }
94
100
 
95
- (() => {
101
+ (async () => {
96
102
  return init();
97
103
  })();
98
104
 
99
- function init() {
100
- program.description(description);
101
- program.version(version);
105
+ async function init() {
106
+ const program = new Command();
107
+ const info = await getPackageInfo();
108
+ program.description(info.description);
109
+ program.version(info.version);
102
110
  program.option(
103
111
  '-b, --build',
104
112
  'build project utility files & package dependencies'
@@ -111,17 +119,16 @@ function init() {
111
119
  '-s, --sync',
112
120
  'sync new instance-based src files to the ts directory'
113
121
  );
114
- program.parse(process.argv).opts();
115
- return hasApplication() && getOption(program.opts());
122
+ return hasApplication() && getOption(program);
116
123
  }
117
124
 
118
- function introPrompt(msg) {
125
+ function introPrompt(msg: string) {
119
126
  return intro(msg);
120
127
  }
121
128
 
122
129
  async function runInstall() {
123
130
  const s = startPrompts('Installing packages', null);
124
- return await exec(getFilePath('install.sh'), (stdout) => {
131
+ return await execFile(getFilePath('install.sh'), (stdout) => {
125
132
  stopPrompt(s, 'Packages installed');
126
133
  outro('Completed');
127
134
  return stdout;
@@ -129,19 +136,19 @@ async function runInstall() {
129
136
  }
130
137
 
131
138
  async function runSync() {
132
- return await exec(getFilePath('sync.sh'), (stdout) => {
139
+ return await execFile(getFilePath('sync.sh'), (stdout) => {
133
140
  runInstall();
134
141
  return stdout;
135
142
  });
136
143
  }
137
144
 
138
- function startPrompts(start, intro) {
145
+ function startPrompts(start: string, intro: string) {
139
146
  intro && introPrompt(intro);
140
147
  const s = spinner();
141
148
  s.start(start);
142
149
  return s;
143
150
  }
144
151
 
145
- function stopPrompt(spinner, msg) {
152
+ function stopPrompt(spinner: any, msg: string) {
146
153
  return spinner.stop(msg);
147
154
  }
package/tsconfig.json CHANGED
@@ -1,9 +1,10 @@
1
1
  {
2
2
  "compilerOptions": {
3
- "target": "esnext",
4
- "module": "commonjs",
3
+ "target": "ESNext",
4
+ "module": "NodeNext",
5
5
  "esModuleInterop": true,
6
6
  "outDir": "bin",
7
+ "resolveJsonModule": true,
7
8
  "rootDir": "src",
8
9
  "skipLibCheck": true
9
10
  },