Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

publish to jsr #16

Merged
merged 5 commits into from
Feb 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 7 additions & 9 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,24 @@ on:
- main

env:
DENO_VERSION: "1.31.1"
NODE_VERSION: "18.15.0"
DENO_VERSION: "1.41.0"
NODE_VERSION: "20.11.1"

jobs:
test:
runs-on: ubuntu-latest
timeout-minutes: 10
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4

- name: setup node
uses: actions/setup-node@v3
uses: actions/setup-node@v4
with:
node-version: ${{ env.NODE_VERSION }}

- name: cache node_modules
id: node_modules_cache_id
uses: actions/cache@v3
uses: actions/cache@v4
with:
path: ~/.npm
key: node-v${{ env.NODE_VERSION }}-deps-${{ hashFiles('**/package-lock.json') }}
Expand All @@ -41,11 +41,9 @@ jobs:
deno-version: ${{ env.DENO_VERSION }}

- name: Cache Deno dependencies
uses: actions/cache@v3
uses: actions/cache@v4
with:
path: |
/home/runner/.deno
/home/runner/.cache/deno
path: ~/.cache/deno
key: deno-v${{ env.DENO_VERSION }}-${{ hashFiles('**/deno.lock') }}
restore-keys: |
deno-v${{ env.DENO_VERSION }}-
Expand Down
16 changes: 16 additions & 0 deletions .github/workflows/publish.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
name: Publish to jsr

on:
release:
types: [published]

jobs:
publish:
runs-on: ubuntu-latest
permissions:
contents: read # need for checkout
id-token: write # The OIDC ID token is used for authentication with JSR.
steps:
- uses: actions/checkout@v4
- uses: denoland/setup-deno@v1
- run: deno publish
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,9 @@ Please install [Deno](https://deno.land/manual@v1.30.3/getting_started/installat
## command
### remote
- dry run
- `deno run --allow-env --allow-read --allow-write https://deno.land/x/module_specifier_resolver@v1.0.19/bin.ts -b=./src -c=./tsconfig.json -d`
- `deno run --allow-env --allow-read --allow-write https://deno.land/x/module_specifier_resolver@v1.0.20/bin.ts -b=./src -c=./tsconfig.json -d`
- transform
- `deno run --allow-env --allow-read --allow-write https://deno.land/x/module_specifier_resolver@v1.0.19/bin.ts -b=./src -c=./tsconfig.json -r`
- `deno run --allow-env --allow-read --allow-write https://deno.land/x/module_specifier_resolver@v1.0.20/bin.ts -b=./src -c=./tsconfig.json -r`
### local
- `deno task run-dry`
- `deno task run`
Expand Down
206 changes: 204 additions & 2 deletions bin.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,205 @@
import { main } from "./src/bin_internal.ts";
import { cli, io, path, ts, walk } from './src/deps.ts';
import {
hasShouldResolveImportedFiles,
resolvedModules,
} from './src/resolve_util.ts';
import { preserveNewLine, restoreNewLine } from './src/str.ts';
import { transform } from './src/transform.ts';

await main();
const flags = cli.parse(Deno.args, {
string: ['b', 'c'],
boolean: ['d', 'r'],
});

const main = async (args: {
basePath?: string;
options?: {
tsConfigPath?: string;
dryRun?: boolean;
repl?: boolean;
};
} = {
basePath: flags.b,
options: {
tsConfigPath: flags.c,
dryRun: flags.d ?? false,
repl: flags.r ?? false,
},
}) => {
const { basePath, options } = args;
const _basePath = basePath ?? '.';
const tsConfigPath = options?.tsConfigPath ?? './tsconfig.json';
const match = [/\.ts$/, /\.mts$/, /\.jsx$/, /\.tsx$/];
const skip = [/node_modules/];
const decoder = new TextDecoder('utf-8');
const tsConfigJson = await Deno.readFile(tsConfigPath);
const tsConfigObject = ts.parseJsonConfigFileContent(
JSON.parse(decoder.decode(tsConfigJson)),
ts.sys,
_basePath,
);
const printer = ts.createPrinter();

const transformedList: Array<{
path: string;
result: string;
}> = [];

console.log('processing...');

for await (const entry of walk(_basePath, { match, skip })) {
if (entry.isFile) {
const targetPath = entry.path;
const targetFileAbsPath = path.resolve(targetPath);
const fileContent = preserveNewLine(decoder.decode(
await Deno.readFile(targetFileAbsPath),
));

const { importedFiles } = ts.preProcessFile(fileContent, true, true);

if (
!hasShouldResolveImportedFiles({
importedFiles,
targetFileAbsPath,
tsConfigObject,
})
) continue;

const imports = resolvedModules({
importedFiles,
targetFileAbsPath,
tsConfigObject,
});

const sourceFile = ts.createSourceFile(
targetFileAbsPath,
fileContent,
ts.ScriptTarget.ESNext,
true,
);

const result = restoreNewLine(
// It seems like ts.Printer.printNode doesn't keep original source newline.🤔
transform({
sourceFile,
imports,
tsConfigObject,
printer,
}),
tsConfigObject.options.newLine,
);
transformedList.push({
path: targetFileAbsPath,
result,
});
}
}

if (transformedList.length === 0) {
console.log(
`%cThere're no transform target files.`,
'color: green',
);
Deno.exit();
}
console.log(
`%ctransform target ${transformedList.length} files found.`,
'color: yellow',
);
const encoder = new TextEncoder();

const writeFiles = async (): Promise<void> => {
await Promise.all(transformedList.map((transformed) => {
return new Promise((resolve, reject) => {
const { path, result } = transformed;
try {
resolve(
Deno.writeFile(
path,
encoder.encode(result),
).then(),
);
} catch (error) {
reject(error);
}
});
})).then(() => {
console.log(
`%cupdate ${transformedList.length} files, finished.`,
'color: green',
);
}).catch((error) => {
throw new Error(error);
});
};

const LOG_FILE_NAME = 'module-specifier-resolver.log';

const writeLog = async (): Promise<void> => {
// try remove log file if it exsist or not
try {
await Deno.remove(LOG_FILE_NAME);
} catch (_) { /* noop */ }
await Promise.all(transformedList.map((transformed) => {
return new Promise((resolve, reject) => {
const { path, result } = transformed;
try {
resolve(
Deno.writeFile(
LOG_FILE_NAME,
encoder.encode(
`file: ${path}
${result}

`,
),
{
append: true,
},
).then(),
);
} catch (error) {
reject(error);
}
});
})).then(() => {
console.log(
`%cDry run: ${transformedList.length} files, finished.`,
'color: green',
);
console.log(
`%cInfo: ${LOG_FILE_NAME}`,
'color: blue',
);
}).catch((error) => {
throw new Error(error);
});
};

if (options?.dryRun) {
await writeLog();
Deno.exit();
}

if (options?.repl) {
console.log(
`%cAre you sure complement the extension of module specifier to files? (y/n)`,
'color: yellow',
);
for await (const line of io.readLines(Deno.stdin)) {
if (line.trim().toLowerCase() === 'y') {
await writeFiles();
Deno.exit();
} else {
Deno.exit();
}
}
} else {
await writeFiles();
Deno.exit();
}
};

if(import.meta.main) {
await main();
}
3 changes: 3 additions & 0 deletions deno.jsonc
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
{
"name": "@hajime-san/module-specifier-resolver",
"version": "1.0.20",
"exports": "./bin.ts",
"tasks": {
"run-dry": "deno run --allow-env --allow-read --allow-write bin.ts -b=./examples/repo/src -c=./examples/repo/tsconfig.json -d",
"run": "deno run --allow-env --allow-read --allow-write bin.ts -b=./examples/repo/src -c=./examples/repo/tsconfig.json -r",
Expand Down
Loading