@langri-sha/projen-lint-synthesized 0.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/CHANGELOG.json +34 -0
- package/CHANGELOG.md +18 -0
- package/license +20 -0
- package/package.json +26 -0
- package/readme +9 -0
- package/src/__snapshots__/index.test.ts.snap +23 -0
- package/src/index.test.ts +80 -0
- package/src/index.ts +102 -0
- package/tsconfig.json +9 -0
package/CHANGELOG.json
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@langri-sha/projen-lint-synthesized",
|
|
3
|
+
"entries": [
|
|
4
|
+
{
|
|
5
|
+
"date": "Tue, 30 Apr 2024 23:02:26 GMT",
|
|
6
|
+
"version": "0.1.0",
|
|
7
|
+
"tag": "@langri-sha/projen-lint-synthesized_v0.1.0",
|
|
8
|
+
"comments": {
|
|
9
|
+
"minor": [
|
|
10
|
+
{
|
|
11
|
+
"author": "filip.dupanovic@gmail.com",
|
|
12
|
+
"package": "@langri-sha/projen-lint-synthesized",
|
|
13
|
+
"commit": "5b2b47f068f116b65549bd1b0c0a68c455ee319b",
|
|
14
|
+
"comment": "feat(projen-lint-synthesized): Configure ESM support"
|
|
15
|
+
},
|
|
16
|
+
{
|
|
17
|
+
"author": "filip.dupanovic@gmail.com",
|
|
18
|
+
"package": "@langri-sha/projen-lint-synthesized",
|
|
19
|
+
"commit": "5b2b47f068f116b65549bd1b0c0a68c455ee319b",
|
|
20
|
+
"comment": "Add initial support for linting synthesized files"
|
|
21
|
+
}
|
|
22
|
+
],
|
|
23
|
+
"patch": [
|
|
24
|
+
{
|
|
25
|
+
"author": "filip.dupanovic@gmail.com",
|
|
26
|
+
"package": "@langri-sha/projen-lint-synthesized",
|
|
27
|
+
"commit": "5b2b47f068f116b65549bd1b0c0a68c455ee319b",
|
|
28
|
+
"comment": "fix(lint-synthesized): Add missing spaces between filenames"
|
|
29
|
+
}
|
|
30
|
+
]
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
]
|
|
34
|
+
}
|
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
# Change Log - @langri-sha/projen-lint-synthesized
|
|
2
|
+
|
|
3
|
+
This log was last generated on Tue, 30 Apr 2024 23:02:26 GMT and should not be manually modified.
|
|
4
|
+
|
|
5
|
+
<!-- Start content -->
|
|
6
|
+
|
|
7
|
+
## 0.1.0
|
|
8
|
+
|
|
9
|
+
Tue, 30 Apr 2024 23:02:26 GMT
|
|
10
|
+
|
|
11
|
+
### Minor changes
|
|
12
|
+
|
|
13
|
+
- feat(projen-lint-synthesized): Configure ESM support (filip.dupanovic@gmail.com)
|
|
14
|
+
- Add initial support for linting synthesized files (filip.dupanovic@gmail.com)
|
|
15
|
+
|
|
16
|
+
### Patches
|
|
17
|
+
|
|
18
|
+
- fix(lint-synthesized): Add missing spaces between filenames (filip.dupanovic@gmail.com)
|
package/license
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 Filip Dupanović <filip.dupanovic@gmail.com> (https://langri-sha.com)
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
|
6
|
+
this software and associated documentation files (the "Software"), to deal in
|
|
7
|
+
the Software without restriction, including without limitation the rights to
|
|
8
|
+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
|
9
|
+
the Software, and to permit persons to whom the Software is furnished to do so,
|
|
10
|
+
subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
|
17
|
+
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
|
18
|
+
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
|
19
|
+
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
|
20
|
+
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
package/package.json
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@langri-sha/projen-lint-synthesized",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"license": "MIT",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "src/index.ts",
|
|
7
|
+
"dependencies": {
|
|
8
|
+
"debug": "4.3.4",
|
|
9
|
+
"minimatch": "9.0.4"
|
|
10
|
+
},
|
|
11
|
+
"devDependencies": {
|
|
12
|
+
"@langri-sha/jest-test": "0.2.0",
|
|
13
|
+
"@langri-sha/tsconfig": "0.2.0",
|
|
14
|
+
"@types/debug": "4.1.12",
|
|
15
|
+
"execa": "8.0.1",
|
|
16
|
+
"prettier": "3.2.5",
|
|
17
|
+
"projen": "0.81.1"
|
|
18
|
+
},
|
|
19
|
+
"peerDependencies": {
|
|
20
|
+
"projen": "^0.81.1",
|
|
21
|
+
"typescript": "^5.0.0"
|
|
22
|
+
},
|
|
23
|
+
"publishConfig": {
|
|
24
|
+
"access": "public"
|
|
25
|
+
}
|
|
26
|
+
}
|
package/readme
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
# @langri-sha/projen-lint-synthesized
|
|
2
|
+
|
|
3
|
+
A [projen] component that you can add to your project to configure linters for
|
|
4
|
+
synthesized files.
|
|
5
|
+
|
|
6
|
+
This is especially uself for workspaces that are migrating to projen, or rely on
|
|
7
|
+
existing lint configurations.
|
|
8
|
+
|
|
9
|
+
[projen]: https://projen.io/
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
// Jest Snapshot v1, https://goo.gl/fbAQLP
|
|
2
|
+
|
|
3
|
+
exports[`defaults 1`] = `
|
|
4
|
+
{
|
|
5
|
+
".gitignore": "# ~~ Generated by projen. To modify, edit .projenrc.js and run "npx projen".
|
|
6
|
+
node_modules/
|
|
7
|
+
!/.gitattributes
|
|
8
|
+
!/.projen/tasks.json
|
|
9
|
+
!/.projen/deps.json
|
|
10
|
+
!/.projen/files.json
|
|
11
|
+
",
|
|
12
|
+
".projen/files.json": {
|
|
13
|
+
"//": "~~ Generated by projen. To modify, edit .projenrc.js and run "npx projen".",
|
|
14
|
+
"files": [
|
|
15
|
+
".gitattributes",
|
|
16
|
+
".gitignore",
|
|
17
|
+
".projen/deps.json",
|
|
18
|
+
".projen/files.json",
|
|
19
|
+
".projen/tasks.json",
|
|
20
|
+
],
|
|
21
|
+
},
|
|
22
|
+
}
|
|
23
|
+
`;
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
import { directorySnapshot } from 'projen/lib/util/synth'
|
|
2
|
+
import { expect, tempy, test } from '@langri-sha/jest-test'
|
|
3
|
+
|
|
4
|
+
import { promises as fs } from 'node:fs'
|
|
5
|
+
import path from 'node:path'
|
|
6
|
+
|
|
7
|
+
import { Project, TextFile } from 'projen'
|
|
8
|
+
import { LintSynthesized, type LintSynthesizedOptions } from './index'
|
|
9
|
+
|
|
10
|
+
const setup = (options?: LintSynthesizedOptions) => {
|
|
11
|
+
const outdir = tempy.directory()
|
|
12
|
+
|
|
13
|
+
const project = new Project({
|
|
14
|
+
name: 'test-project',
|
|
15
|
+
outdir,
|
|
16
|
+
})
|
|
17
|
+
|
|
18
|
+
project.removeTask('build')
|
|
19
|
+
project.removeTask('compile')
|
|
20
|
+
project.removeTask('eject')
|
|
21
|
+
project.removeTask('default')
|
|
22
|
+
project.removeTask('package')
|
|
23
|
+
project.removeTask('post-compile')
|
|
24
|
+
project.removeTask('pre-compile')
|
|
25
|
+
project.removeTask('test')
|
|
26
|
+
|
|
27
|
+
new LintSynthesized(project, options)
|
|
28
|
+
|
|
29
|
+
return { project }
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
test('defaults', () => {
|
|
33
|
+
const { project } = setup()
|
|
34
|
+
|
|
35
|
+
project.synth()
|
|
36
|
+
expect(directorySnapshot(project.outdir)).toMatchSnapshot()
|
|
37
|
+
})
|
|
38
|
+
|
|
39
|
+
test('lints synthesized files', async () => {
|
|
40
|
+
const { project } = setup({
|
|
41
|
+
'*': 'prettier --ignore-unknown --write',
|
|
42
|
+
})
|
|
43
|
+
|
|
44
|
+
let file, contents
|
|
45
|
+
|
|
46
|
+
file = new TextFile(project, 'test1.js')
|
|
47
|
+
file.addLine(`module.exports = ${JSON.stringify({ foo: 'bar' })}`)
|
|
48
|
+
|
|
49
|
+
file = new TextFile(project, 'test2.js')
|
|
50
|
+
file.addLine(`module.exports = ${JSON.stringify({ foo: 'bar' })}`)
|
|
51
|
+
|
|
52
|
+
project.synth()
|
|
53
|
+
|
|
54
|
+
file = await fs.readFile(path.join(project.outdir, 'test1.js'))
|
|
55
|
+
contents = file.toString('utf8')
|
|
56
|
+
|
|
57
|
+
expect(contents).toEqual(`module.exports = { foo: "bar" };\n`)
|
|
58
|
+
|
|
59
|
+
file = await fs.readFile(path.join(project.outdir, 'test2.js'))
|
|
60
|
+
contents = file.toString('utf8')
|
|
61
|
+
|
|
62
|
+
expect(contents).toEqual(`module.exports = { foo: "bar" };\n`)
|
|
63
|
+
})
|
|
64
|
+
|
|
65
|
+
test('preserves file modes', async () => {
|
|
66
|
+
const { project } = setup({
|
|
67
|
+
'*': 'prettier --ignore-unknown',
|
|
68
|
+
})
|
|
69
|
+
|
|
70
|
+
new TextFile(project, 'test.sh', {
|
|
71
|
+
executable: true,
|
|
72
|
+
readonly: true,
|
|
73
|
+
})
|
|
74
|
+
|
|
75
|
+
project.synth()
|
|
76
|
+
|
|
77
|
+
await expect(
|
|
78
|
+
fs.stat(path.join(project.outdir, 'test.sh')),
|
|
79
|
+
).resolves.toHaveProperty('mode', 33_124)
|
|
80
|
+
})
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
import { Component } from 'projen'
|
|
2
|
+
import createDebug from 'debug'
|
|
3
|
+
|
|
4
|
+
import * as fs from 'node:fs'
|
|
5
|
+
|
|
6
|
+
import { execaSync } from 'execa'
|
|
7
|
+
import { minimatch } from 'minimatch'
|
|
8
|
+
|
|
9
|
+
const debug = createDebug('projen-lint-synthesized')
|
|
10
|
+
|
|
11
|
+
export interface LintSynthesizedOptions {
|
|
12
|
+
[pattern: string]: string | ((files: string[]) => string)
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* A component that lints synthesized files.
|
|
17
|
+
*/
|
|
18
|
+
export class LintSynthesized extends Component {
|
|
19
|
+
#options?: LintSynthesizedOptions
|
|
20
|
+
|
|
21
|
+
constructor(
|
|
22
|
+
scope: ConstructorParameters<typeof Component>[0],
|
|
23
|
+
options?: LintSynthesizedOptions,
|
|
24
|
+
) {
|
|
25
|
+
super(scope, 'lint-synthesized')
|
|
26
|
+
|
|
27
|
+
this.#options = options
|
|
28
|
+
|
|
29
|
+
debug('Initialized')
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
override postSynthesize(): void {
|
|
33
|
+
super.postSynthesize()
|
|
34
|
+
|
|
35
|
+
debug('Commencing lints on synthesized files')
|
|
36
|
+
|
|
37
|
+
const fileInfo = Object.fromEntries(
|
|
38
|
+
this.project.files
|
|
39
|
+
.filter((file) => fs.existsSync(file.absolutePath))
|
|
40
|
+
.map((file) => [
|
|
41
|
+
file.path,
|
|
42
|
+
{
|
|
43
|
+
file,
|
|
44
|
+
absolutePath: file.absolutePath,
|
|
45
|
+
mode: fs.statSync(file.absolutePath).mode,
|
|
46
|
+
},
|
|
47
|
+
]),
|
|
48
|
+
)
|
|
49
|
+
const paths = Object.keys(fileInfo)
|
|
50
|
+
|
|
51
|
+
debug(`Found ${paths.length} synthesized files`)
|
|
52
|
+
|
|
53
|
+
const tasks = Object.entries(this.#options || {})
|
|
54
|
+
.map(([pattern, command]) => ({
|
|
55
|
+
pattern,
|
|
56
|
+
command,
|
|
57
|
+
files: minimatch.match(paths, pattern),
|
|
58
|
+
}))
|
|
59
|
+
.filter(({ files }) => files.length)
|
|
60
|
+
|
|
61
|
+
for (const { files } of tasks) {
|
|
62
|
+
for (const file of files) {
|
|
63
|
+
const { absolutePath } = fileInfo[file]
|
|
64
|
+
|
|
65
|
+
fs.chmodSync(absolutePath, 0o755)
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
for (const { pattern, command, files } of tasks) {
|
|
70
|
+
debug(`Running command ${files.length} files matching pattern ${pattern}`)
|
|
71
|
+
|
|
72
|
+
try {
|
|
73
|
+
this.#run(files, command)
|
|
74
|
+
} catch {
|
|
75
|
+
// Let `execa` log errors.
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
for (const { files } of tasks) {
|
|
80
|
+
for (const file of files) {
|
|
81
|
+
const { absolutePath, mode } = fileInfo[file]
|
|
82
|
+
|
|
83
|
+
fs.chmodSync(absolutePath, mode)
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
#run(files: string[], command: string | ((files: string[]) => string)) {
|
|
89
|
+
const task =
|
|
90
|
+
typeof command === 'string'
|
|
91
|
+
? `${command} ${files.join(' ')}`
|
|
92
|
+
: command(files)
|
|
93
|
+
|
|
94
|
+
debug(`Running command ${task}`)
|
|
95
|
+
|
|
96
|
+
execaSync(task, {
|
|
97
|
+
shell: true,
|
|
98
|
+
stdio: 'inherit',
|
|
99
|
+
cwd: this.project.outdir,
|
|
100
|
+
})
|
|
101
|
+
}
|
|
102
|
+
}
|
package/tsconfig.json
ADDED