Skip to content

Commit

Permalink
Update package (#99)
Browse files Browse the repository at this point in the history
* chore(packages): update to latest non-breaking versions

* chore(packages): update to latest breaking versions
  • Loading branch information
GervinFung authored Sep 29, 2024
1 parent f9b57ad commit 8ca2f9a
Show file tree
Hide file tree
Showing 10 changed files with 389 additions and 434 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/action.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ jobs:
strategy:
fail-fast: false
matrix:
node: [18, 20]
os: [ubuntu-latest, windows-latest]
node-version: [18, 20, 22]
threads: [4]

name: Test with Node ${{ matrix.node-version }} on ${{ matrix.os }}
Expand All @@ -20,7 +20,7 @@ jobs:

- uses: pnpm/action-setup@v4
with:
version: 8
version: 9
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v3
with:
Expand Down
5 changes: 2 additions & 3 deletions eslint.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,8 @@ import tseslint from 'typescript-eslint';
export default tseslint.config(
includeIgnoreFile(`${process.cwd()}/.gitignore`),
eslint.configs.recommended,
...tseslint.configs.recommendedTypeChecked,
...tseslint.configs.strict,
...tseslint.configs.stylistic,
...tseslint.configs.strictTypeChecked,
...tseslint.configs.stylisticTypeChecked,
node,
{
ignores: ['test/process/source/**', 'test/process/expected-result/**'],
Expand Down
10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,19 +40,19 @@
"url": "https://github.com/GervinFung/ts-add-js-extension"
},
"dependencies": {
"typescript": "^5.5.4"
"typescript": "^5.6.2"
},
"devDependencies": {
"@poolofdeath20/eslint-config": "^0.4.0",
"@poolofdeath20/eslint-config": "^0.4.1",
"@poolofdeath20/prettier-config-generator": "^0.0.1",
"@poolofdeath20/tsconfig": "^0.1.1",
"@types/node": "^22.5.4",
"@types/node": "^22.7.4",
"eslint": "^9.10.0",
"node-package-helper": "github:GervinFung/node-package-helper",
"prettier": "^3.3.3",
"shx": "^0.3.4",
"vite-node": "^2.0.5",
"vitest": "^2.0.5"
"vite-node": "^2.1.1",
"vitest": "^2.1.1"
},
"keywords": [
"esm",
Expand Down
725 changes: 323 additions & 402 deletions pnpm-lock.yaml

Large diffs are not rendered by default.

59 changes: 43 additions & 16 deletions src/cli-command-parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,13 @@ class TokenParser {

readonly parseVersion = () => {
const { keyword } = commandKeyWords.version;

if (this.token.key !== keyword) {
return {
exists: false,
} as const;
}

return {
type: keyword,
exists: true,
Expand All @@ -52,11 +54,13 @@ class TokenParser {

readonly parseHelp = () => {
const { keyword } = commandKeyWords.help;

if (this.token.key !== commandKeyWords.help.keyword) {
return {
exists: false,
} as const;
}

return {
type: keyword,
exists: true,
Expand All @@ -66,6 +70,7 @@ class TokenParser {

readonly parseDir = () => {
const { keyword } = commandKeyWords.dir;

if (this.token.key !== commandKeyWords.dir.keyword) {
return {
exists: false,
Expand All @@ -86,6 +91,7 @@ class TokenParser {

readonly parseInclude = () => {
const { keyword } = commandKeyWords.include;

if (this.token.key !== commandKeyWords.include.keyword) {
return {
exists: false,
Expand All @@ -101,7 +107,9 @@ class TokenParser {

readonly parseShowChanges = () => {
const { keyword } = commandKeyWords.showChanges;

const { key, value } = this.token;

if (key !== commandKeyWords.showChanges.keyword) {
return {
exists: false,
Expand Down Expand Up @@ -146,35 +154,44 @@ class ParseArgs {

static readonly create = (arg: string) => {
const tokens = arg.split(commandKeyWords.assignment.key);
const name = ParseArgs.checkPackageName(tokens.at(0));
if (name !== 'proceed') {
throw name.error;

const result = ParseArgs.checkPackageName(tokens.at(0));

if (result.isInvalid) {
throw result.error;
}

if (tokens.includes('add')) {
console.log(
'The "add" in the command can be removed, as it is only used for backward compatibility'
);
}

return new this(
tokens
.map((token) => {
return token === 'add' ? '' : token;
})
.filter((_, index) => {
return index;
})
.slice(1)
);
};

private static readonly checkPackageName = (name: string | undefined) => {
return name?.includes(pkg.name ?? '')
? 'proceed'
: {
const packageName = guard({
value: name,
error: new Error('The pkg name is undefined'),
});

return packageName.includes(pkg.name)
? ({
isInvalid: false,
} as const)
: ({
isInvalid: true,
error: new Error(
`The pkg name "${name}" passed is invalid`
`The pkg name "${packageName}" passed is invalid`
),
};
} as const);
};

private readonly tokenize = (args: Args) => {
Expand All @@ -186,10 +203,18 @@ class ParseArgs {
return [arg];
}

const [key, ...value] = arg.split(' ');
const [nullableKey, ...value] = arg.split(' ');

const key = guard({
value: nullableKey,
error: new Error(
'Key cannot be undefined after being split'
),
});

return [`${key}${assign}${value.join(' ')}`];
})
.map((args): Token => {
.map((args) => {
const [key, value] = args.split(assign);

return {
Expand All @@ -205,7 +230,7 @@ class ParseArgs {
'Value cannot be undefined after being flat mapped'
),
}).trim(),
};
} as Token;
});
};

Expand All @@ -217,7 +242,9 @@ class ParseArgs {
}
return new TokenParser(token).parseVersion();
},
{ exists: false } as ReturnType<TokenParser['parseVersion']>
{
exists: false,
} as ReturnType<TokenParser['parseVersion']>
);
};

Expand Down Expand Up @@ -265,7 +292,7 @@ class ParseArgs {
return node.type !== 'invalid' ? [] : [node];
})
.forEach((node) => {
return console.log(
console.log(
`The "${JSON.stringify(node.token, undefined, 4)}" in the command is invalid as ${node.reason}. So please remove it`
);
});
Expand Down
2 changes: 1 addition & 1 deletion src/const.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const matchDts = (filePath: string) => {
};

const matchEither = (filePath: string) => {
return matchJs(filePath) || matchDts(filePath);
return matchJs(filePath) ?? matchDts(filePath);
};

export { extensions, matchJs, matchDts, matchEither, separator };
5 changes: 4 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { writeMany, findMany } from './read-write';

const tsAddJsExtension = async ({
config,
// eslint-disable-next-line @typescript-eslint/no-deprecated
parsedConfigFunction,
}: Readonly<{
config: PartialConfig;
Expand Down Expand Up @@ -73,7 +74,9 @@ const main = (args: string) => {
}
}
})
.catch(console.error);
.catch((error: unknown) => {
console.error(error);
});
};

export { parseConfig, tsAddJsExtension };
Expand Down
2 changes: 1 addition & 1 deletion src/read-write.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ const writeMany = async (
}>
) => {
const repeat = props.withJSExtension.reduce((longestFileName, { file }) => {
return longestFileName?.length <= file.length ? file : longestFileName;
return longestFileName.length <= file.length ? file : longestFileName;
}, '').length;

const log = !(props.withJSExtension.length && props.showChanges)
Expand Down
9 changes: 6 additions & 3 deletions src/type.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,25 @@
const guard = <T, Err extends Error>(
const guard = <T>(
props: Readonly<{
value: T;
error: Err;
error: Error;
}>
) => {
const t = props.value;

if (t !== undefined && t !== null) {
return t;
}
// eslint-disable-next-line @typescript-eslint/only-throw-error

throw props.error;
};

const asString = (props: Parameters<typeof guard>[0]) => {
const s = props.value;

if (typeof s === 'string') {
return s;
}

throw props.error;
};

Expand Down
2 changes: 2 additions & 0 deletions test/config/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { parseConfig } from '../../src';
describe('Config parsing', () => {
it('should throw error when parsing config the old way', () => {
expect(() => {
// eslint-disable-next-line @typescript-eslint/no-deprecated
return parseConfig({
dir: 'dir',
include: ['hi'],
Expand All @@ -14,6 +15,7 @@ describe('Config parsing', () => {

it('should parse config when only non optional config options are given', () => {
expect(() => {
// eslint-disable-next-line @typescript-eslint/no-deprecated
return parseConfig({
dir: 'dir',
});
Expand Down

0 comments on commit 8ca2f9a

Please sign in to comment.