@vanillaes/esmtk 0.6.6 → 0.7.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.
@@ -1,10 +1,27 @@
1
+ import { installed, which } from '../../src/util.js'
1
2
  import { spawn } from 'child_process'
2
- import { join } from 'path'
3
-
4
- const BIN_PATH = join(process.cwd(), 'node_modules', '.bin', 'esbuild')
5
3
 
4
+ /**
5
+ * Bundle ESM (ECMAScript Module) code (with tree-shaking)
6
+ * @param {string} input the input path
7
+ * @param {string} output the output path
8
+ */
6
9
  export async function bundle (input, output) {
7
- spawn(BIN_PATH, ['--format=esm', '--bundle', input, `--outfile=${output}`], {
10
+ const npmExists = await which('npm')
11
+ if (!npmExists) {
12
+ console.error('npm not found')
13
+ console.error('is node installed?')
14
+ return
15
+ }
16
+
17
+ const esbuildExists = await installed('esbuild')
18
+ if (!esbuildExists) {
19
+ console.error('esbuild not found')
20
+ console.error('esbuild can be installed with `npm i -g esbuild`')
21
+ return
22
+ }
23
+
24
+ spawn('esbuild', ['--format=esm', '--bundle', input, `--outfile=${output}`], {
8
25
  cwd: process.cwd(),
9
26
  stdio: ['pipe', process.stdout, process.stderr]
10
27
  }).on('error', err => {
@@ -1,10 +1,27 @@
1
+ import { installed, which } from '../../src/util.js'
1
2
  import { spawn } from 'child_process'
2
- import { join } from 'path'
3
-
4
- const BIN_PATH = join(process.cwd(), 'node_modules', '.bin', 'esbuild')
5
3
 
4
+ /**
5
+ * Bundle CJS (CommonJS) code
6
+ * @param {string} input the input path
7
+ * @param {string} output the output path
8
+ */
6
9
  export async function commonjs (input, output) {
7
- spawn(BIN_PATH, ['--format=cjs', '--bundle', input, `--outfile=${output}`], {
10
+ const npmExists = await which('npm')
11
+ if (!npmExists) {
12
+ console.error('npm not found')
13
+ console.error('is node installed?')
14
+ return
15
+ }
16
+
17
+ const esbuildExists = await installed('esbuild')
18
+ if (!esbuildExists) {
19
+ console.error('esbuild not found')
20
+ console.error('esbuild can be installed with `npm i -g esbuild`')
21
+ return
22
+ }
23
+
24
+ spawn('esbuild', ['--format=cjs', '--bundle', input, `--outfile=${output}`], {
8
25
  cwd: process.cwd(),
9
26
  stdio: ['pipe', process.stdout, process.stderr]
10
27
  }).on('error', err => {
@@ -8,7 +8,7 @@ export async function lint (flags) {
8
8
  cwd: process.cwd(),
9
9
  stdio: ['pipe', 'pipe', 'pipe']
10
10
  }).on('error', err => {
11
- console.error(`test${err}`)
11
+ console.error(`${err}`)
12
12
  })
13
13
 
14
14
  child.stdout.on('data', (data) => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vanillaes/esmtk",
3
- "version": "0.6.6",
3
+ "version": "0.7.1",
4
4
  "description": "ES Module Toolkit",
5
5
  "keywords": [
6
6
  "esm",
@@ -28,7 +28,6 @@
28
28
  },
29
29
  "dependencies": {
30
30
  "commander": "^14.0.3",
31
- "esbuild": "^0.27.3",
32
31
  "standard": "^17.1.2"
33
32
  },
34
33
  "engines": {
package/src/util.js ADDED
@@ -0,0 +1,32 @@
1
+ import { exec } from 'child_process'
2
+ import { promisify } from 'node:util'
3
+
4
+ const execAsync = promisify(exec)
5
+
6
+ /**
7
+ * Check to see if a NPM package is installed globally
8
+ * @param {string} pkg the name of the package
9
+ * @returns {boolean} true if the package is installed, false otherwise
10
+ */
11
+ export async function installed (pkg) {
12
+ try {
13
+ await execAsync(`npm list -g --depth=0 ${pkg}`)
14
+ return true
15
+ } catch (e) {
16
+ return false
17
+ }
18
+ }
19
+
20
+ /**
21
+ * Check to see if an application is installed globally
22
+ * @param {string} program the name of the application
23
+ * @returns {boolean} true if the application is installed, false otherwise
24
+ */
25
+ export async function which (program) {
26
+ try {
27
+ await execAsync(`which ${program}`)
28
+ return true
29
+ } catch (e) {
30
+ return false
31
+ }
32
+ }