js-build-tools 4.1.1 → 5.1.0
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/README.md +274 -267
- package/babel.config.js +18 -1
- package/dist/build.js +58 -56
- package/dist/build.min.js +1 -1
- package/dist/bundle.js +16 -16
- package/dist/bundleLint.js +16 -20
- package/dist/bundleLint.min.js +1 -1
- package/dist/bundleMinify.js +13 -13
- package/dist/compileReadme.js +10 -10
- package/dist/copyFonts.js +13 -13
- package/dist/default.js +15 -15
- package/dist/dist.js +10 -10
- package/dist/distLint.js +16 -20
- package/dist/distLint.min.js +1 -1
- package/dist/distMinify.js +13 -13
- package/dist/images.js +10 -10
- package/dist/partials/addToReadme.js +17 -17
- package/dist/partials/beginWatcher.js +12 -12
- package/dist/partials/clean.js +14 -14
- package/dist/partials/copyFor.js +12 -9
- package/dist/partials/distFor.js +21 -21
- package/dist/partials/distForSrc.js +11 -11
- package/dist/partials/distSeries.js +19 -19
- package/dist/partials/imagesFor.js +56 -21
- package/dist/partials/minifyFor.js +16 -16
- package/dist/partials/readmeTemplate.js +14 -14
- package/dist/partials/removeDirectory.js +37 -13
- package/dist/partials/runOnChange.js +28 -28
- package/dist/partials/sassFor.js +28 -22
- package/dist/partials/standardLint.js +96 -0
- package/dist/partials.js +110 -103
- package/dist/partials.min.js +1 -1
- package/dist/sass.js +10 -10
- package/dist/test-helpers/setUp.js +89 -89
- package/dist/test-helpers/standardLintWithRealStandard.js +28 -0
- package/dist/testFull.js +3 -3
- package/dist/testHelpers.js +10 -10
- package/dist/testQuick.js +3 -3
- package/dist/typeScript.js +9 -9
- package/dist/utilities/dotGet.js +46 -46
- package/dist/utilities/dotNotate.js +46 -46
- package/dist/utilities/dotSet.js +37 -37
- package/dist/utilities/isObject.js +8 -8
- package/dist/utilities/strAfter.js +11 -11
- package/dist/utilities/strBefore.js +11 -11
- package/dist/watchFull.js +10 -10
- package/dist/watchTest.js +16 -16
- package/functions/build.mjs +6 -3
- package/functions/build.test.mjs +31 -0
- package/functions/bundleLint.mjs +2 -6
- package/functions/bundleLint.test.mjs +3 -0
- package/functions/distLint.mjs +2 -6
- package/functions/distLint.test.mjs +3 -0
- package/functions/partials/copyFor.mjs +2 -1
- package/functions/partials/copyFor.test.mjs +18 -0
- package/functions/partials/imagesFor.mjs +35 -3
- package/functions/partials/imagesFor.test.mjs +83 -1
- package/functions/partials/removeDirectory.mjs +28 -14
- package/functions/partials/removeDirectory.test.mjs +44 -0
- package/functions/partials/sassFor.mjs +5 -2
- package/functions/partials/sassFor.test.mjs +22 -4
- package/functions/partials/standardLint.mjs +80 -0
- package/functions/partials/standardLint.test.mjs +99 -0
- package/functions/partials.mjs +1 -0
- package/functions/partials.test.mjs +1 -1
- package/functions/test-helpers/standardLintWithRealStandard.mjs +21 -0
- package/package.json +15 -4
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
import fs from 'fs'
|
|
2
2
|
import * as setUp from '../test-helpers/setUp.mjs'
|
|
3
|
-
import
|
|
3
|
+
import os from 'os'
|
|
4
|
+
import zlib from 'zlib'
|
|
5
|
+
import { imagesFor, loadImagemin } from './imagesFor.mjs'
|
|
4
6
|
|
|
5
7
|
setUp.setDefaults('test-images-for')
|
|
6
8
|
const gulpConfig = setUp.gulpConfig
|
|
@@ -116,6 +118,44 @@ const imageContents = '<?xml version="1.0" encoding="UTF-8" standalone="no"?>\n'
|
|
|
116
118
|
|
|
117
119
|
const imageCompressed = '<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="600" height="600"><image width="600" height="600" preserveAspectRatio="none" xlink:href="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAKAAAACgCAYAAACLz2ctAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz AAADsQAAA7EB9YPtSQAAABl0RVh0U29mdHdhcmUAd3d3Lmlua3NjYXBlLm9yZ5vuPBoAAAhrSURB VHic7d3fi1x3Gcfx97M7TdKYYNPYbLaxl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x+l8JfBT4gZl9fsRlpLqy+DV3/1eiWjImut8DnqYY6QbpAI+a2eNmtn+34uWu+/4a/XXT6DeB OgDlZeKfG2G+B4ATZvZbdz/f6wlmdj/F7jyFl/otR9rN3B0z+z5wb8Ual4CzwBvAMsWVxg9S7HpT fPrxNvCzKgdAMv46Znac6uGD4tLu04n62e4m8ITCN7mmgLuimxjgyQpH3dIiHXZeqz8OHHjK3Z+P bkTy6pDuHF0qDvzG3c9GNyL5TQEXopvosg78QuHbOzrAq9FNlK4Cv3L3l6MbkeZ03P2qmV1mhC+U yeAcxQHHcmAPEmDzk5AzQcu/QXGa5ZcK395kxe9qg5mdAk41tNw14FngaXdfamiZMoa2AghgZl8H vphxee9Q/E7G0+6+mHE50hLbA2jA14CHSHt6Zg54EfiLu9f5ZRuZMO8K4NadxdeHPUzx7UZVP8+9 TBG6F919vnKHMtF6BnDrQbNZ4LMU3yY1w84v7naKX6C+QXFRwhubk7vfyNGwTJaBAdzxZLMDwBGK g4glYMVHKSCyzUgBFEmtzd+OJRNAAZRQCqCEUgAllAIooRRACaUASigFUEIpgBJKAZRQCqCEUgAl lAIooRRACaUASigFUEIpgBJKAZRQCqCEUgAllAIooRRACaUASigFUEL9D1MqMi7YkUEVAAAAAElF TkSuQmCC"/><path d="M19.04 494.382C2.001 482.76-2.83 468.022 5.006 451.587c3.27-6.858 10.156-15.228 15.3-18.599 8.904-5.834 9.948-5.675 21.728 3.31 28.014 21.367 44.09 18.882 51.548-7.971 5.951-21.426 5.252-305.141-.77-313.044-2.617-3.433-11.316-6.65-19.688-7.28-14.39-1.082-15.047-1.7-16.164-15.19L55.797 78.75h326.107l2.574 11.718c2.93 13.34-1.711 18.133-17.648 18.225-19.29.11-21.83 7.13-21.83 60.325V217.5h135v-48.169c0-41.902-.892-48.976-6.858-54.375-3.772-3.413-10.384-6.206-14.693-6.206-15.006 0-19.699-4.116-19.699-17.276V78.75h161.703l-1.164 14.063c-1.132 13.677-1.605 14.094-17.28 15.224-13.45.97-16.737 2.798-19.876 11.053-5.802 15.26-2.837 249.42 3.25 256.747 2.617 3.15 11.477 6.801 19.688 8.114C599.596 386.274 600 386.745 600 401.37v15.03l-79.687-1.012-79.688-1.012-1.137-13.607c-1.135-13.594-1.12-13.61 16.875-16.305 9.907-1.484 19.252-4.642 20.767-7.018 1.515-2.376 2.781-30.054 2.813-61.508L480 258.75H344.814l1.03 60.938 1.031 60.937 19.688 3.34c20.492 3.476 22.165 5.78 17.323 23.848-2.253 8.408-2.53 8.437-80.573 8.437H225v-14.956c0-14.536.418-15.022 14.93-17.343 8.21-1.313 17.07-4.966 19.687-8.119 3.637-4.381 4.758-35.56 4.758-132.343 0-69.636-1.23-127.842-2.735-129.348-9.738-9.743-68.924-8.348-79.154 1.866-3.087 3.081-4.993 46.77-6.525 149.486-1.455 97.609-3.646 149.68-6.69 159.02-13.551 41.573-61.403 74.912-111 77.337-22.755 1.112-28.167.082-39.232-7.468z"/></svg>'
|
|
118
120
|
|
|
121
|
+
// Build a small but real PNG so the test exercises binary data (a text-only SVG hid Gulp 5's utf8 corruption of images).
|
|
122
|
+
const crc32 = buffer => {
|
|
123
|
+
let crc = 0xffffffff
|
|
124
|
+
for (const byte of buffer) {
|
|
125
|
+
crc ^= byte
|
|
126
|
+
for (let bit = 0; bit < 8; bit++) {
|
|
127
|
+
crc = (crc >>> 1) ^ (0xedb88320 & -(crc & 1))
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
return (crc ^ 0xffffffff) >>> 0
|
|
131
|
+
}
|
|
132
|
+
const pngChunk = (type, data) => {
|
|
133
|
+
const length = Buffer.alloc(4)
|
|
134
|
+
length.writeUInt32BE(data.length)
|
|
135
|
+
const body = Buffer.concat([Buffer.from(type), data])
|
|
136
|
+
const crc = Buffer.alloc(4)
|
|
137
|
+
crc.writeUInt32BE(crc32(body))
|
|
138
|
+
return Buffer.concat([length, body, crc])
|
|
139
|
+
}
|
|
140
|
+
const makePng = (size = 32) => {
|
|
141
|
+
const header = Buffer.alloc(13)
|
|
142
|
+
header.writeUInt32BE(size, 0)
|
|
143
|
+
header.writeUInt32BE(size, 4)
|
|
144
|
+
header.set([8, 2, 0, 0, 0], 8) // 8-bit RGB
|
|
145
|
+
const rows = Buffer.alloc((size * 3 + 1) * size)
|
|
146
|
+
for (let y = 0; y < size; y++) {
|
|
147
|
+
for (let x = 0; x < size * 3; x++) {
|
|
148
|
+
rows[y * (size * 3 + 1) + 1 + x] = (x * 37 + y * 91) % 256 // bytes above 0x7f exercise utf8 corruption
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
return Buffer.concat([
|
|
152
|
+
Buffer.from([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a]),
|
|
153
|
+
pngChunk('IHDR', header),
|
|
154
|
+
pngChunk('IDAT', zlib.deflateSync(rows)),
|
|
155
|
+
pngChunk('IEND', Buffer.alloc(0))
|
|
156
|
+
])
|
|
157
|
+
}
|
|
158
|
+
|
|
119
159
|
beforeEach(setUp.beforeEach)
|
|
120
160
|
|
|
121
161
|
afterEach(setUp.afterEach)
|
|
@@ -141,4 +181,46 @@ describe('imagesFor', () => {
|
|
|
141
181
|
done()
|
|
142
182
|
})
|
|
143
183
|
})
|
|
184
|
+
|
|
185
|
+
test('keeps binary images intact - Gulp 5 would otherwise decode them as utf8 text and corrupt or drop them', done => {
|
|
186
|
+
const srcPath = gulpConfig.get('srcPath')
|
|
187
|
+
const pngFile = `${srcPath}/binary.png`
|
|
188
|
+
const original = makePng()
|
|
189
|
+
fs.writeFileSync(pngFile, original)
|
|
190
|
+
expect.assertions(4)
|
|
191
|
+
const browserPath = gulpConfig.get('browser.to')
|
|
192
|
+
imagesFor(pngFile, browserPath)
|
|
193
|
+
.on('finish', () => {
|
|
194
|
+
const outputPath = `${browserPath}/binary.png`
|
|
195
|
+
expect(fs.existsSync(outputPath)).toBeTruthy()
|
|
196
|
+
const optimized = fs.readFileSync(outputPath)
|
|
197
|
+
// Still a real PNG (signature and IEND intact) and never bigger than what went in.
|
|
198
|
+
expect(optimized.subarray(0, 8).equals(original.subarray(0, 8))).toBeTruthy()
|
|
199
|
+
expect(optimized.subarray(-12).equals(original.subarray(-12))).toBeTruthy()
|
|
200
|
+
expect(optimized.length).toBeLessThanOrEqual(original.length)
|
|
201
|
+
done()
|
|
202
|
+
})
|
|
203
|
+
.on('error', error => {
|
|
204
|
+
console.error('Encountered error', error)
|
|
205
|
+
done()
|
|
206
|
+
})
|
|
207
|
+
})
|
|
208
|
+
})
|
|
209
|
+
|
|
210
|
+
describe('loadImagemin', () => {
|
|
211
|
+
test('loads the gulp-imagemin plugin factory from the project directory', () => {
|
|
212
|
+
expect.assertions(1)
|
|
213
|
+
expect(typeof loadImagemin()).toBe('function')
|
|
214
|
+
})
|
|
215
|
+
|
|
216
|
+
test('throws a helpful error with install instructions when gulp-imagemin is not installed', () => {
|
|
217
|
+
expect.assertions(2)
|
|
218
|
+
const emptyProject = fs.mkdtempSync(`${os.tmpdir()}/no-imagemin-`)
|
|
219
|
+
try {
|
|
220
|
+
expect(() => loadImagemin(emptyProject)).toThrow(/optional peer dependency gulp-imagemin, which is not installed/)
|
|
221
|
+
expect(() => loadImagemin(emptyProject)).toThrow(/github:jheagle\/gulp-imagemin#common-js-compatibility/)
|
|
222
|
+
} finally {
|
|
223
|
+
fs.rmSync(emptyProject, { recursive: true })
|
|
224
|
+
}
|
|
225
|
+
})
|
|
144
226
|
})
|
|
@@ -1,20 +1,34 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { access, rm } from 'node:fs/promises'
|
|
2
|
+
import { setTimeout as sleep } from 'node:timers/promises'
|
|
3
|
+
|
|
4
|
+
const attempts = 4
|
|
5
|
+
|
|
6
|
+
const exists = dirPath => access(dirPath).then(() => true, () => false)
|
|
2
7
|
|
|
3
8
|
/**
|
|
4
|
-
*
|
|
9
|
+
* Delete a directory and everything in it, resolving only once it is really gone.
|
|
10
|
+
*
|
|
11
|
+
* A single rm can leave a directory behind - most often when something is still writing into it (a build task, a
|
|
12
|
+
* watcher, a dev server) or has it briefly locked (EBUSY/EPERM/ENOTEMPTY) - which silently leaves stale output for
|
|
13
|
+
* the next build. So: Node's own retry-with-back-off handles the transient errors, then the directory is checked and
|
|
14
|
+
* removed again if it was recreated, and a clear error is raised rather than carrying on with stale output.
|
|
5
15
|
* @memberOf module:partials
|
|
6
16
|
* @param {string} dirPath
|
|
7
|
-
* @returns {Promise
|
|
17
|
+
* @returns {Promise<string>} Resolves with dirPath once the directory no longer exists (or already didn't).
|
|
18
|
+
* @throws {Error} If the directory still exists after several attempts.
|
|
8
19
|
*/
|
|
9
|
-
export const removeDirectory = dirPath =>
|
|
10
|
-
(
|
|
11
|
-
dirPath,
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
20
|
+
export const removeDirectory = async dirPath => {
|
|
21
|
+
for (let attempt = 1; attempt <= attempts; attempt++) {
|
|
22
|
+
await rm(dirPath, { recursive: true, force: true, maxRetries: 5, retryDelay: 100 })
|
|
23
|
+
if (!await exists(dirPath)) {
|
|
24
|
+
return dirPath
|
|
25
|
+
}
|
|
26
|
+
if (attempt < attempts) {
|
|
27
|
+
await sleep(200 * attempt)
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
throw new Error(
|
|
31
|
+
`Unable to remove "${dirPath}" - it was recreated or is locked. Stop anything that has it open or is writing ` +
|
|
32
|
+
'into it (watchers, dev servers, editors, other build tasks) and try again.'
|
|
19
33
|
)
|
|
20
|
-
|
|
34
|
+
}
|
|
@@ -1,6 +1,26 @@
|
|
|
1
1
|
import fs from 'fs'
|
|
2
|
+
import { rm } from 'node:fs/promises'
|
|
2
3
|
import { removeDirectory } from './removeDirectory.mjs'
|
|
3
4
|
|
|
5
|
+
// Wrap rm so tests can simulate something recreating the directory while (or right after) it is deleted.
|
|
6
|
+
jest.mock('node:fs/promises', () => {
|
|
7
|
+
const actual = jest.requireActual('node:fs/promises')
|
|
8
|
+
return { ...actual, rm: jest.fn(actual.rm) }
|
|
9
|
+
})
|
|
10
|
+
const { rm: realRm } = jest.requireActual('node:fs/promises')
|
|
11
|
+
|
|
12
|
+
const dirPath = 'remove-directory'
|
|
13
|
+
const recreateAfterRemoving = async (target, options) => {
|
|
14
|
+
await realRm(target, options)
|
|
15
|
+
fs.mkdirSync(target)
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
afterEach(() => {
|
|
19
|
+
rm.mockReset()
|
|
20
|
+
rm.mockImplementation(realRm)
|
|
21
|
+
fs.rmSync(dirPath, { recursive: true, force: true })
|
|
22
|
+
})
|
|
23
|
+
|
|
4
24
|
describe('removeDirectory', () => {
|
|
5
25
|
test('able to delete the remove-directory', async () => {
|
|
6
26
|
const dirPath = 'remove-directory'
|
|
@@ -25,4 +45,28 @@ describe('removeDirectory', () => {
|
|
|
25
45
|
expect(fs.existsSync(dirPath)).toBeFalsy()
|
|
26
46
|
expect(await removeDirectory(dirPath)).toBe(dirPath)
|
|
27
47
|
})
|
|
48
|
+
|
|
49
|
+
test('removes with force and Node\'s built-in retry-with-back-off for busy or locked directories', async () => {
|
|
50
|
+
expect.assertions(1)
|
|
51
|
+
fs.mkdirSync(dirPath)
|
|
52
|
+
await removeDirectory(dirPath)
|
|
53
|
+
expect(rm).toHaveBeenCalledWith(dirPath, expect.objectContaining({ recursive: true, force: true, maxRetries: 5 }))
|
|
54
|
+
})
|
|
55
|
+
|
|
56
|
+
test('removes the directory again when something recreates it right after the first removal', async () => {
|
|
57
|
+
expect.assertions(2)
|
|
58
|
+
fs.mkdirSync(dirPath)
|
|
59
|
+
rm.mockImplementationOnce(recreateAfterRemoving)
|
|
60
|
+
await removeDirectory(dirPath)
|
|
61
|
+
expect(fs.existsSync(dirPath)).toBeFalsy()
|
|
62
|
+
expect(rm).toHaveBeenCalledTimes(2)
|
|
63
|
+
})
|
|
64
|
+
|
|
65
|
+
test('fails with a clear error instead of leaving stale output when the directory keeps coming back', async () => {
|
|
66
|
+
expect.assertions(2)
|
|
67
|
+
fs.mkdirSync(dirPath)
|
|
68
|
+
rm.mockImplementation(recreateAfterRemoving)
|
|
69
|
+
await expect(removeDirectory(dirPath)).rejects.toThrow(/Unable to remove "remove-directory" - it was recreated or is locked/)
|
|
70
|
+
expect(rm).toHaveBeenCalledTimes(4)
|
|
71
|
+
})
|
|
28
72
|
})
|
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
import { dest, src } from 'gulp'
|
|
2
2
|
import * as gulpConfig from '../../gulp.config.mjs'
|
|
3
|
-
import
|
|
3
|
+
import autoprefixer from 'autoprefixer'
|
|
4
|
+
import cssnano from 'cssnano'
|
|
5
|
+
import postcss from 'gulp-postcss'
|
|
4
6
|
import rename from 'gulp-rename'
|
|
5
7
|
import * as sass from 'sass'
|
|
6
8
|
import { default as gulpSass } from 'gulp-sass'
|
|
@@ -17,6 +19,7 @@ const runSass = gulpSass(sass)
|
|
|
17
19
|
export const sassFor = (srcSearch = gulpConfig.get('sass.from'), cssPath = gulpConfig.get('sass.to')) => src(srcSearch)
|
|
18
20
|
.pipe(runSass().on('error', runSass.logError)) // Passes it through a gulp-sass, log errors to console
|
|
19
21
|
.pipe(dest(cssPath))
|
|
20
|
-
.
|
|
22
|
+
// add: false - the old gulp-cssnano only ever removed obsolete vendor prefixes, never added any; keep that.
|
|
23
|
+
.pipe(postcss([autoprefixer({ add: false }), cssnano()]))
|
|
21
24
|
.pipe(rename({ extname: '.min.css' }))
|
|
22
25
|
.pipe(dest(cssPath))
|
|
@@ -27,10 +27,7 @@ const sassContents = 'html\n' +
|
|
|
27
27
|
|
|
28
28
|
const sassPath = gulpConfig.get('sass.path')
|
|
29
29
|
|
|
30
|
-
beforeEach(() => {
|
|
31
|
-
setUp.beforeEach()
|
|
32
|
-
return fs.mkdirSync(sassPath, { recursive: true })
|
|
33
|
-
})
|
|
30
|
+
beforeEach(() => setUp.beforeEach().then(() => fs.mkdirSync(sassPath, { recursive: true })))
|
|
34
31
|
|
|
35
32
|
afterEach(setUp.afterEach)
|
|
36
33
|
|
|
@@ -60,4 +57,25 @@ describe('sassFor', () => {
|
|
|
60
57
|
done()
|
|
61
58
|
})
|
|
62
59
|
})
|
|
60
|
+
|
|
61
|
+
test('also writes a minified .min.css next to the css output', done => {
|
|
62
|
+
const sassFile = `${sassPath}/sassFor.sass`
|
|
63
|
+
fs.writeFileSync(sassFile, sassContents)
|
|
64
|
+
expect.assertions(4)
|
|
65
|
+
const cssPath = gulpConfig.get('sass.to')
|
|
66
|
+
sassFor(sassFile)
|
|
67
|
+
.on('finish', () => {
|
|
68
|
+
const cssContents = fs.readFileSync(`${cssPath}/sassFor.css`).toString()
|
|
69
|
+
const minContents = fs.readFileSync(`${cssPath}/sassFor.min.css`).toString()
|
|
70
|
+
expect(minContents.length).toBeLessThan(cssContents.length)
|
|
71
|
+
expect(countMatches(minContents.trim(), '\n')).toEqual(0)
|
|
72
|
+
expect(minContents).toContain('outline:none')
|
|
73
|
+
expect(minContents).toContain('text-align:center')
|
|
74
|
+
done()
|
|
75
|
+
})
|
|
76
|
+
.on('error', error => {
|
|
77
|
+
console.error('Encountered error', error)
|
|
78
|
+
done()
|
|
79
|
+
})
|
|
80
|
+
})
|
|
63
81
|
})
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
import path from 'node:path'
|
|
2
|
+
import through from 'through2'
|
|
3
|
+
|
|
4
|
+
// `standard` (17+) ships ESM-only, but this package also publishes a CommonJS build (dist/*.js) whose babel
|
|
5
|
+
// transform would turn even a dynamic import() into a require() that cannot load an ES module. Hiding the import()
|
|
6
|
+
// inside a Function constructor keeps it a genuine dynamic import that works from either build (same escape hatch
|
|
7
|
+
// as tsFor.mjs uses for gulp-ts-compile).
|
|
8
|
+
const importStandard = new Function('return import(\'standard\')')
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Print a file's lint messages, if it has any.
|
|
12
|
+
* @memberOf module:partials
|
|
13
|
+
* @param {import('vinyl')} file
|
|
14
|
+
* @param {{messages: Array<{line: number, column: number, message: string}>, errorCount: number, warningCount: number}} result
|
|
15
|
+
* @returns {undefined}
|
|
16
|
+
*/
|
|
17
|
+
const printFileReport = (file, result) => {
|
|
18
|
+
if (!result.messages.length) {
|
|
19
|
+
return
|
|
20
|
+
}
|
|
21
|
+
console.log(path.relative(file.cwd, file.path))
|
|
22
|
+
result.messages.forEach(({ line, column, message }) => console.log(`line ${line}:${column}\t${message}`))
|
|
23
|
+
console.log(`✖ ${result.errorCount} error\t⚠ ${result.warningCount} warning\n`)
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Lint every file streaming through with JavaScript Standard Style and, unless `fix` is false, apply the automatic
|
|
28
|
+
* fixes to the file's contents so a following `dest()` writes the fixed version. Remaining problems are printed
|
|
29
|
+
* along with a summary once the stream ends; they never fail the build.
|
|
30
|
+
*
|
|
31
|
+
* Project-level Standard configuration (the `standard` field in package.json: ignore, env, globals, ...) is honored
|
|
32
|
+
* because the file's own path and cwd are handed to Standard.
|
|
33
|
+
* @memberOf module:partials
|
|
34
|
+
* @param {Object} [options={}]
|
|
35
|
+
* @param {boolean} [options.fix=true] - Apply Standard's automatic fixes to the file contents.
|
|
36
|
+
* @param {function(): Promise<{lintText: Function}>} [loadStandard] - Resolves the `standard` module. Real callers
|
|
37
|
+
* should never need to pass this - it exists so tests can supply a stand-in, since this project's
|
|
38
|
+
* CommonJS-transform-based Jest setup cannot execute a real dynamic import() of an ES module.
|
|
39
|
+
* @returns {import('stream').Transform}
|
|
40
|
+
*/
|
|
41
|
+
export const standardLint = (
|
|
42
|
+
{ fix = true } = {},
|
|
43
|
+
loadStandard = () => importStandard().then(({ default: standard }) => standard)
|
|
44
|
+
) => {
|
|
45
|
+
const totals = { errors: 0, warnings: 0 }
|
|
46
|
+
return through.obj(
|
|
47
|
+
function (file, enc, callback) {
|
|
48
|
+
if (file.isNull()) {
|
|
49
|
+
callback(null, file)
|
|
50
|
+
return
|
|
51
|
+
}
|
|
52
|
+
if (file.isStream()) {
|
|
53
|
+
callback(new Error('standardLint: streaming file contents are not supported.'))
|
|
54
|
+
return
|
|
55
|
+
}
|
|
56
|
+
loadStandard()
|
|
57
|
+
.then(standard => standard.lintText(file.contents.toString(), { filename: file.path, cwd: file.cwd, fix }))
|
|
58
|
+
.then(results => {
|
|
59
|
+
// No result at all means the file is ignored by the project's Standard configuration.
|
|
60
|
+
const [result] = results
|
|
61
|
+
if (result) {
|
|
62
|
+
if (typeof result.output === 'string') {
|
|
63
|
+
file.contents = Buffer.from(result.output)
|
|
64
|
+
}
|
|
65
|
+
totals.errors += result.errorCount
|
|
66
|
+
totals.warnings += result.warningCount
|
|
67
|
+
printFileReport(file, result)
|
|
68
|
+
}
|
|
69
|
+
callback(null, file)
|
|
70
|
+
}, callback)
|
|
71
|
+
},
|
|
72
|
+
function (callback) {
|
|
73
|
+
console.log('Standard linter results')
|
|
74
|
+
console.log('======================================')
|
|
75
|
+
console.log(`✖ Errors total: ${totals.errors}`)
|
|
76
|
+
console.log(`⚠ Warnings total: ${totals.warnings}\n`)
|
|
77
|
+
callback()
|
|
78
|
+
}
|
|
79
|
+
)
|
|
80
|
+
}
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
import Vinyl from 'vinyl'
|
|
2
|
+
import { Readable } from 'node:stream'
|
|
3
|
+
import { standardLint } from './standardLint.mjs'
|
|
4
|
+
|
|
5
|
+
const makeFile = (contents, filePath = '/project/dist/example.js') => new Vinyl({
|
|
6
|
+
cwd: '/project',
|
|
7
|
+
base: '/project/dist',
|
|
8
|
+
path: filePath,
|
|
9
|
+
contents
|
|
10
|
+
})
|
|
11
|
+
|
|
12
|
+
const run = (transform, files) => new Promise((resolve, reject) => {
|
|
13
|
+
const out = []
|
|
14
|
+
transform.on('data', file => out.push(file)).on('end', () => resolve(out)).on('error', reject)
|
|
15
|
+
files.forEach(file => transform.write(file))
|
|
16
|
+
transform.end()
|
|
17
|
+
})
|
|
18
|
+
|
|
19
|
+
const fakeStandard = (resultFor) => ({
|
|
20
|
+
lintText: jest.fn((text, options) => Promise.resolve(resultFor(text, options)))
|
|
21
|
+
})
|
|
22
|
+
|
|
23
|
+
let logSpy
|
|
24
|
+
|
|
25
|
+
beforeEach(() => {
|
|
26
|
+
logSpy = jest.spyOn(console, 'log').mockImplementation(() => {})
|
|
27
|
+
})
|
|
28
|
+
|
|
29
|
+
afterEach(() => {
|
|
30
|
+
logSpy.mockRestore()
|
|
31
|
+
})
|
|
32
|
+
|
|
33
|
+
describe('standardLint', () => {
|
|
34
|
+
test('replaces file contents with the fixed output and hands Standard the file path, cwd and fix option', async () => {
|
|
35
|
+
expect.assertions(3)
|
|
36
|
+
const standard = fakeStandard((text) => [{ messages: [], errorCount: 0, warningCount: 0, output: text.replace(/"/g, '\'') }])
|
|
37
|
+
const [linted] = await run(standardLint({ fix: true }, () => Promise.resolve(standard)), [makeFile(Buffer.from('const a = "b"'))])
|
|
38
|
+
expect(linted.contents.toString()).toEqual('const a = \'b\'')
|
|
39
|
+
expect(standard.lintText).toHaveBeenCalledWith('const a = "b"', { filename: '/project/dist/example.js', cwd: '/project', fix: true })
|
|
40
|
+
expect(linted.path).toEqual('/project/dist/example.js')
|
|
41
|
+
})
|
|
42
|
+
|
|
43
|
+
test('leaves the contents alone when Standard has no fixed output (nothing needed fixing)', async () => {
|
|
44
|
+
expect.assertions(1)
|
|
45
|
+
const standard = fakeStandard(() => [{ messages: [], errorCount: 0, warningCount: 0 }])
|
|
46
|
+
const [linted] = await run(standardLint({}, () => Promise.resolve(standard)), [makeFile(Buffer.from('const a = 1'))])
|
|
47
|
+
expect(linted.contents.toString()).toEqual('const a = 1')
|
|
48
|
+
})
|
|
49
|
+
|
|
50
|
+
test('passes fix: false through so nothing is rewritten', async () => {
|
|
51
|
+
expect.assertions(1)
|
|
52
|
+
const standard = fakeStandard(() => [{ messages: [], errorCount: 0, warningCount: 0 }])
|
|
53
|
+
await run(standardLint({ fix: false }, () => Promise.resolve(standard)), [makeFile(Buffer.from('x'))])
|
|
54
|
+
expect(standard.lintText.mock.calls[0][1].fix).toBe(false)
|
|
55
|
+
})
|
|
56
|
+
|
|
57
|
+
test('passes a file through untouched when the project ignores it (Standard returns no results)', async () => {
|
|
58
|
+
expect.assertions(2)
|
|
59
|
+
const standard = fakeStandard(() => [])
|
|
60
|
+
const [linted] = await run(standardLint({}, () => Promise.resolve(standard)), [makeFile(Buffer.from('var x=1'))])
|
|
61
|
+
expect(linted.contents.toString()).toEqual('var x=1')
|
|
62
|
+
expect(logSpy).toHaveBeenCalledWith('✖ Errors total: 0')
|
|
63
|
+
})
|
|
64
|
+
|
|
65
|
+
test('skips null-content files without linting them', async () => {
|
|
66
|
+
expect.assertions(2)
|
|
67
|
+
const standard = fakeStandard(() => [])
|
|
68
|
+
const [passed] = await run(standardLint({}, () => Promise.resolve(standard)), [makeFile(null)])
|
|
69
|
+
expect(passed.isNull()).toBeTruthy()
|
|
70
|
+
expect(standard.lintText).not.toHaveBeenCalled()
|
|
71
|
+
})
|
|
72
|
+
|
|
73
|
+
test('errors on streaming file contents, which cannot be linted', async () => {
|
|
74
|
+
expect.assertions(1)
|
|
75
|
+
const streamFile = makeFile(Readable.from(['x']))
|
|
76
|
+
await expect(run(standardLint({}, () => Promise.resolve(fakeStandard(() => []))), [streamFile])).rejects.toThrow(/streaming/)
|
|
77
|
+
})
|
|
78
|
+
|
|
79
|
+
test('prints per-file problems and a summary of totals across all files, without failing', async () => {
|
|
80
|
+
expect.assertions(5)
|
|
81
|
+
const standard = fakeStandard((text, { filename }) => filename.endsWith('a.js')
|
|
82
|
+
? [{ messages: [{ line: 2, column: 7, message: 'unused b' }, { line: 3, column: 1, message: 'bad thing' }], errorCount: 2, warningCount: 0 }]
|
|
83
|
+
: [{ messages: [{ line: 1, column: 1, message: 'just a warning' }], errorCount: 0, warningCount: 1 }])
|
|
84
|
+
const out = await run(
|
|
85
|
+
standardLint({}, () => Promise.resolve(standard)),
|
|
86
|
+
[makeFile(Buffer.from('a'), '/project/dist/a.js'), makeFile(Buffer.from('b'), '/project/dist/b.js')]
|
|
87
|
+
)
|
|
88
|
+
expect(out).toHaveLength(2)
|
|
89
|
+
expect(logSpy).toHaveBeenCalledWith('dist/a.js')
|
|
90
|
+
expect(logSpy).toHaveBeenCalledWith('line 2:7\tunused b')
|
|
91
|
+
expect(logSpy).toHaveBeenCalledWith('✖ Errors total: 2')
|
|
92
|
+
expect(logSpy).toHaveBeenCalledWith('⚠ Warnings total: 1\n')
|
|
93
|
+
})
|
|
94
|
+
|
|
95
|
+
test('surfaces a failure to load or run Standard as a stream error', async () => {
|
|
96
|
+
expect.assertions(1)
|
|
97
|
+
await expect(run(standardLint({}, () => Promise.reject(new Error('cannot load'))), [makeFile(Buffer.from('x'))])).rejects.toThrow('cannot load')
|
|
98
|
+
})
|
|
99
|
+
})
|
package/functions/partials.mjs
CHANGED
|
@@ -20,4 +20,5 @@ export { readmeTemplate } from './partials/readmeTemplate.mjs'
|
|
|
20
20
|
export { removeDirectory } from './partials/removeDirectory.mjs'
|
|
21
21
|
export { runOnChange } from './partials/runOnChange.mjs'
|
|
22
22
|
export { sassFor } from './partials/sassFor.mjs'
|
|
23
|
+
export { standardLint } from './partials/standardLint.mjs'
|
|
23
24
|
export { tsFor } from './partials/tsFor.mjs'
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { execFileSync } from 'node:child_process'
|
|
2
|
+
|
|
3
|
+
// Jest's CommonJS transform cannot load `standard` (ESM-only) or run a real dynamic import(), so tests that need
|
|
4
|
+
// genuine Standard behavior run the actual `standard` package in a plain Node subprocess instead of faking it.
|
|
5
|
+
const script = 'import { readFileSync } from \'node:fs\'\n' +
|
|
6
|
+
'import standard from \'standard\'\n' +
|
|
7
|
+
'const { text, options } = JSON.parse(readFileSync(0, \'utf8\'))\n' +
|
|
8
|
+
'process.stdout.write(JSON.stringify(await standard.lintText(text, options)))\n'
|
|
9
|
+
|
|
10
|
+
export const loadStandardViaSubprocess = () => Promise.resolve({
|
|
11
|
+
lintText: (text, options) => Promise.resolve().then(() => JSON.parse(execFileSync(
|
|
12
|
+
process.execPath,
|
|
13
|
+
['--input-type=module', '--eval', script],
|
|
14
|
+
{ input: JSON.stringify({ text, options }), encoding: 'utf8', cwd: process.cwd(), maxBuffer: 256 * 1024 * 1024 }
|
|
15
|
+
)))
|
|
16
|
+
})
|
|
17
|
+
|
|
18
|
+
const actual = jest.requireActual('../partials/standardLint.mjs')
|
|
19
|
+
|
|
20
|
+
// Drop-in replacement for the partial module (see jest.mock in the lint/build tests): same API, real Standard.
|
|
21
|
+
export const standardLint = (options) => actual.standardLint(options, loadStandardViaSubprocess)
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "js-build-tools",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "5.1.0",
|
|
4
4
|
"description": "Standardised build process with gulp.",
|
|
5
5
|
"main": "gulpfile.base.js",
|
|
6
6
|
"exports": {
|
|
@@ -51,29 +51,39 @@
|
|
|
51
51
|
"@babel/preset-env": "^7.29.7",
|
|
52
52
|
"@babel/preset-typescript": "^7.29.7",
|
|
53
53
|
"@babel/runtime-corejs3": "^8.0.6",
|
|
54
|
+
"autoprefixer": "^10.6.1",
|
|
54
55
|
"babel-jest": "^30.4.1",
|
|
55
56
|
"browserify": "^17.0.1",
|
|
56
57
|
"core-js": "^3.49.0",
|
|
58
|
+
"cssnano": "^6.1.2",
|
|
57
59
|
"glob": "^13.0.6",
|
|
58
60
|
"gulp": "^5.0.1",
|
|
59
61
|
"gulp-babel": "^8.1.0",
|
|
60
62
|
"gulp-cache": "^1.1.3",
|
|
61
|
-
"gulp-
|
|
62
|
-
"gulp-imagemin": "github:jheagle/gulp-imagemin#common-js-compatibility",
|
|
63
|
+
"gulp-postcss": "^10.0.0",
|
|
63
64
|
"gulp-rename": "^2.1.0",
|
|
64
65
|
"gulp-sass": "^6.0.1",
|
|
65
|
-
"gulp-standard": "^14.0.0",
|
|
66
66
|
"gulp-ts-compile": "^1.0.0",
|
|
67
67
|
"gulp-uglify-es": "^3.0.0",
|
|
68
68
|
"jest": "^30.5.2",
|
|
69
69
|
"jest-environment-jsdom": "^30.4.1",
|
|
70
70
|
"jsdoc-to-markdown": "^9.1.3",
|
|
71
|
+
"postcss": "^8.5.28",
|
|
71
72
|
"regenerator-runtime": "^0.14.1",
|
|
72
73
|
"sass": "^1.101.0",
|
|
74
|
+
"standard": "^17.1.2",
|
|
73
75
|
"test-filesystem": "^1.5.0",
|
|
74
76
|
"through2": "^4.0.2",
|
|
75
77
|
"vinyl-source-stream": "^2.0.0"
|
|
76
78
|
},
|
|
79
|
+
"peerDependencies": {
|
|
80
|
+
"gulp-imagemin": ">=9.0.0"
|
|
81
|
+
},
|
|
82
|
+
"peerDependenciesMeta": {
|
|
83
|
+
"gulp-imagemin": {
|
|
84
|
+
"optional": true
|
|
85
|
+
}
|
|
86
|
+
},
|
|
77
87
|
"files": [
|
|
78
88
|
"babel.config.js",
|
|
79
89
|
"dist",
|
|
@@ -86,6 +96,7 @@
|
|
|
86
96
|
],
|
|
87
97
|
"funding": "https://www.paypal.com/donate/?business=LRYR6SZRWKXVY&no_recurring=0&item_name=Let%27s+build+together+today+to+simplify+our+jobs+tomorrow.¤cy_code=CAD",
|
|
88
98
|
"devDependencies": {
|
|
99
|
+
"gulp-imagemin": "github:jheagle/gulp-imagemin#common-js-compatibility",
|
|
89
100
|
"typescript": "^6.0.3"
|
|
90
101
|
}
|
|
91
102
|
}
|