sn-typescript-util 1.1.1 → 1.2.2

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