@astrojs/check 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/LICENSE +21 -0
- package/dist/bin.d.ts +1 -0
- package/dist/bin.js +9 -0
- package/dist/index.d.ts +23 -0
- package/dist/index.js +99 -0
- package/dist/options.d.ts +32 -0
- package/dist/options.js +29 -0
- package/package.json +47 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2022 The Astro Technology Company
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, 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,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/dist/bin.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/dist/bin.js
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import path from 'node:path';
|
|
2
|
+
import { check, parseArgsAsCheckConfig } from './index.js';
|
|
3
|
+
const args = parseArgsAsCheckConfig(process.argv);
|
|
4
|
+
console.info(`Getting diagnostics for Astro files in ${path.resolve(args.root)}...`);
|
|
5
|
+
const result = await check(args);
|
|
6
|
+
if (typeof result === 'boolean') {
|
|
7
|
+
process.exit(result ? 1 : 0);
|
|
8
|
+
}
|
|
9
|
+
//# sourceMappingURL=bin.js.map
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { options } from './options.js';
|
|
2
|
+
/**
|
|
3
|
+
* Given a list of arguments from the command line (such as `process.argv`), return parsed and processed options
|
|
4
|
+
*/
|
|
5
|
+
export declare function parseArgsAsCheckConfig(args: string[]): {
|
|
6
|
+
[x: string]: unknown;
|
|
7
|
+
readonly root: string;
|
|
8
|
+
readonly watch: boolean;
|
|
9
|
+
readonly tsconfig: string | undefined;
|
|
10
|
+
readonly minimumFailingSeverity: "error" | "warning" | "hint";
|
|
11
|
+
readonly minimumSeverity: "error" | "warning" | "hint";
|
|
12
|
+
readonly preserveWatchOutput: boolean;
|
|
13
|
+
_: (string | number)[];
|
|
14
|
+
$0: string;
|
|
15
|
+
};
|
|
16
|
+
export type Flags = Pick<ReturnType<typeof parseArgsAsCheckConfig>, keyof typeof options>;
|
|
17
|
+
export declare function check(flags: Partial<Flags> & {
|
|
18
|
+
watch: true;
|
|
19
|
+
}): Promise<void>;
|
|
20
|
+
export declare function check(flags: Partial<Flags> & {
|
|
21
|
+
watch: false;
|
|
22
|
+
}): Promise<boolean>;
|
|
23
|
+
export declare function check(flags: Partial<Flags>): Promise<boolean | void>;
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
import { AstroCheck } from '@astrojs/language-server';
|
|
2
|
+
import { watch } from 'chokidar';
|
|
3
|
+
import { bold, dim, red, yellow } from 'kleur/colors';
|
|
4
|
+
import { createRequire } from 'node:module';
|
|
5
|
+
import path from 'node:path';
|
|
6
|
+
import yargs from 'yargs';
|
|
7
|
+
import { hideBin } from 'yargs/helpers';
|
|
8
|
+
import { options } from './options.js';
|
|
9
|
+
/**
|
|
10
|
+
* Given a list of arguments from the command line (such as `process.argv`), return parsed and processed options
|
|
11
|
+
*/
|
|
12
|
+
export function parseArgsAsCheckConfig(args) {
|
|
13
|
+
return yargs(hideBin(args)).options(options).parseSync();
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* Print diagnostics according to the given flags, and return whether or not the program should exit with an error code.
|
|
17
|
+
*/
|
|
18
|
+
export async function check(flags) {
|
|
19
|
+
const workspaceRoot = path.resolve(flags.root ?? process.cwd());
|
|
20
|
+
const require = createRequire(import.meta.url);
|
|
21
|
+
const checker = new AstroCheck(workspaceRoot, require.resolve('typescript/lib/tsserverlibrary.js'), flags.tsconfig);
|
|
22
|
+
let req = 0;
|
|
23
|
+
if (flags.watch) {
|
|
24
|
+
function createWatcher(rootPath, extensions) {
|
|
25
|
+
return watch(`${rootPath}/**/*{${extensions.join(',')}}`, {
|
|
26
|
+
ignored: (ignoredPath) => ignoredPath.includes('node_modules'),
|
|
27
|
+
ignoreInitial: true,
|
|
28
|
+
});
|
|
29
|
+
}
|
|
30
|
+
// Dynamically get the list of extensions to watch from the files already included in the project
|
|
31
|
+
const checkedExtensions = Array.from(new Set(checker.project.languageHost.getScriptFileNames().map((fileName) => path.extname(fileName))));
|
|
32
|
+
createWatcher(workspaceRoot, checkedExtensions)
|
|
33
|
+
.on('add', (fileName) => {
|
|
34
|
+
checker.project.fileCreated(fileName);
|
|
35
|
+
update();
|
|
36
|
+
})
|
|
37
|
+
.on('unlink', (fileName) => {
|
|
38
|
+
checker.project.fileDeleted(fileName);
|
|
39
|
+
update();
|
|
40
|
+
})
|
|
41
|
+
.on('change', (fileName) => {
|
|
42
|
+
checker.project.fileUpdated(fileName);
|
|
43
|
+
update();
|
|
44
|
+
});
|
|
45
|
+
}
|
|
46
|
+
async function update() {
|
|
47
|
+
if (!flags.preserveWatchOutput)
|
|
48
|
+
process.stdout.write('\x1Bc');
|
|
49
|
+
await lint();
|
|
50
|
+
}
|
|
51
|
+
async function lint() {
|
|
52
|
+
const currentReq = ++req;
|
|
53
|
+
await new Promise((resolve) => setTimeout(resolve, 100));
|
|
54
|
+
const isCanceled = () => currentReq !== req;
|
|
55
|
+
if (isCanceled())
|
|
56
|
+
return;
|
|
57
|
+
const minimumSeverity = flags.minimumSeverity || 'hint';
|
|
58
|
+
const result = await checker.lint({
|
|
59
|
+
logErrors: {
|
|
60
|
+
level: minimumSeverity,
|
|
61
|
+
},
|
|
62
|
+
cancel: isCanceled,
|
|
63
|
+
});
|
|
64
|
+
console.info([
|
|
65
|
+
bold(`Result (${result.fileChecked} file${result.fileChecked === 1 ? '' : 's'}): `),
|
|
66
|
+
['error', 'warning', 'hint'].includes(minimumSeverity)
|
|
67
|
+
? bold(red(`${result.errors} ${result.errors === 1 ? 'error' : 'errors'}`))
|
|
68
|
+
: undefined,
|
|
69
|
+
['warning', 'hint'].includes(minimumSeverity)
|
|
70
|
+
? bold(yellow(`${result.warnings} ${result.warnings === 1 ? 'warning' : 'warnings'}`))
|
|
71
|
+
: undefined,
|
|
72
|
+
['hint'].includes(minimumSeverity)
|
|
73
|
+
? dim(`${result.hints} ${result.hints === 1 ? 'hint' : 'hints'}\n`)
|
|
74
|
+
: undefined,
|
|
75
|
+
]
|
|
76
|
+
.filter(Boolean)
|
|
77
|
+
.join(`\n${dim('-')} `));
|
|
78
|
+
if (flags.watch) {
|
|
79
|
+
console.info('Watching for changes...');
|
|
80
|
+
}
|
|
81
|
+
else {
|
|
82
|
+
switch (flags.minimumFailingSeverity) {
|
|
83
|
+
case 'error':
|
|
84
|
+
return result.errors > 0;
|
|
85
|
+
case 'warning':
|
|
86
|
+
return result.errors + result.warnings > 0;
|
|
87
|
+
case 'hint':
|
|
88
|
+
return result.errors + result.warnings + result.hints > 0;
|
|
89
|
+
default:
|
|
90
|
+
return result.errors > 0;
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
// Always lint on first run, even in watch mode.
|
|
95
|
+
const lintResult = await lint();
|
|
96
|
+
if (!flags.watch)
|
|
97
|
+
return lintResult;
|
|
98
|
+
}
|
|
99
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
export declare const options: {
|
|
2
|
+
readonly root: {
|
|
3
|
+
readonly type: "string";
|
|
4
|
+
readonly default: string;
|
|
5
|
+
readonly description: "Manually specify a root dir to check in. By default, the current working directory is used.";
|
|
6
|
+
};
|
|
7
|
+
readonly watch: {
|
|
8
|
+
readonly type: "boolean";
|
|
9
|
+
readonly default: false;
|
|
10
|
+
readonly alias: "w";
|
|
11
|
+
};
|
|
12
|
+
readonly tsconfig: {
|
|
13
|
+
readonly type: "string";
|
|
14
|
+
readonly description: "Manually specify a path to a `tsconfig.json` or `jsconfig.json` to use. If not specified, the program will attempt to find a config, if it cannot it'll attempt to automatically infer the project's configuration.";
|
|
15
|
+
readonly default: undefined;
|
|
16
|
+
};
|
|
17
|
+
readonly minimumFailingSeverity: {
|
|
18
|
+
readonly choices: readonly ["error", "warning", "hint"];
|
|
19
|
+
readonly description: "Minimum error severity needed to exit with an error code. Choosing 'hint' will for example cause the program to exit with an error if there's any unfixed hints.";
|
|
20
|
+
readonly default: "error";
|
|
21
|
+
};
|
|
22
|
+
readonly minimumSeverity: {
|
|
23
|
+
readonly choices: readonly ["error", "warning", "hint"];
|
|
24
|
+
readonly description: "Minimum diagnostic severity to show. Choosing `warning` will, for example, show both errors and warnings, but not hints. ";
|
|
25
|
+
readonly default: "hint";
|
|
26
|
+
};
|
|
27
|
+
readonly preserveWatchOutput: {
|
|
28
|
+
readonly type: "boolean";
|
|
29
|
+
readonly description: "If set to false, output won't be cleared between checks in watch mode.";
|
|
30
|
+
readonly default: false;
|
|
31
|
+
};
|
|
32
|
+
};
|
package/dist/options.js
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
export const options = {
|
|
2
|
+
root: {
|
|
3
|
+
type: 'string',
|
|
4
|
+
default: process.cwd(),
|
|
5
|
+
description: 'Manually specify a root dir to check in. By default, the current working directory is used.',
|
|
6
|
+
},
|
|
7
|
+
watch: { type: 'boolean', default: false, alias: 'w' },
|
|
8
|
+
tsconfig: {
|
|
9
|
+
type: 'string',
|
|
10
|
+
description: "Manually specify a path to a `tsconfig.json` or `jsconfig.json` to use. If not specified, the program will attempt to find a config, if it cannot it'll attempt to automatically infer the project's configuration.",
|
|
11
|
+
default: undefined,
|
|
12
|
+
},
|
|
13
|
+
minimumFailingSeverity: {
|
|
14
|
+
choices: ['error', 'warning', 'hint'],
|
|
15
|
+
description: "Minimum error severity needed to exit with an error code. Choosing 'hint' will for example cause the program to exit with an error if there's any unfixed hints.",
|
|
16
|
+
default: 'error',
|
|
17
|
+
},
|
|
18
|
+
minimumSeverity: {
|
|
19
|
+
choices: ['error', 'warning', 'hint'],
|
|
20
|
+
description: 'Minimum diagnostic severity to show. Choosing `warning` will, for example, show both errors and warnings, but not hints. ',
|
|
21
|
+
default: 'hint',
|
|
22
|
+
},
|
|
23
|
+
preserveWatchOutput: {
|
|
24
|
+
type: 'boolean',
|
|
25
|
+
description: "If set to false, output won't be cleared between checks in watch mode.",
|
|
26
|
+
default: false,
|
|
27
|
+
},
|
|
28
|
+
};
|
|
29
|
+
//# sourceMappingURL=options.js.map
|
package/package.json
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@astrojs/check",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"author": "withastro",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"repository": {
|
|
7
|
+
"type": "git",
|
|
8
|
+
"url": "https://github.com/withastro/language-tools",
|
|
9
|
+
"directory": "packages/astro-check"
|
|
10
|
+
},
|
|
11
|
+
"type": "module",
|
|
12
|
+
"main": "dist/index.js",
|
|
13
|
+
"types": "dist/index.d.ts",
|
|
14
|
+
"bin": {
|
|
15
|
+
"astro-check": "./dist/bin.js"
|
|
16
|
+
},
|
|
17
|
+
"files": [
|
|
18
|
+
"bin",
|
|
19
|
+
"dist/**/*.js",
|
|
20
|
+
"dist/**/*.d.ts"
|
|
21
|
+
],
|
|
22
|
+
"dependencies": {
|
|
23
|
+
"@astrojs/language-server": "^2.1.0",
|
|
24
|
+
"chokidar": "^3.5.3",
|
|
25
|
+
"fast-glob": "^3.3.1",
|
|
26
|
+
"kleur": "^4.1.5",
|
|
27
|
+
"yargs": "^17.7.2"
|
|
28
|
+
},
|
|
29
|
+
"devDependencies": {
|
|
30
|
+
"@types/node": "^16.18.26",
|
|
31
|
+
"@types/yargs": "^17.0.24",
|
|
32
|
+
"@types/chai": "^4.3.5",
|
|
33
|
+
"@types/mocha": "^10.0.1",
|
|
34
|
+
"chai": "^4.3.7",
|
|
35
|
+
"mocha": "^10.2.0",
|
|
36
|
+
"tsx": "^3.12.7"
|
|
37
|
+
},
|
|
38
|
+
"peerDependencies": {
|
|
39
|
+
"typescript": "^5.0.0"
|
|
40
|
+
},
|
|
41
|
+
"scripts": {
|
|
42
|
+
"build": "tsc",
|
|
43
|
+
"dev": "tsc --watch",
|
|
44
|
+
"test": "mocha --timeout 30000 --require tsx test/**/*.test.ts",
|
|
45
|
+
"test:match": "pnpm run test -g"
|
|
46
|
+
}
|
|
47
|
+
}
|