Skip to content

Commit

Permalink
Use native line endings in project files
Browse files Browse the repository at this point in the history
  • Loading branch information
Hilzu committed Jul 15, 2024
1 parent 3e4d1c0 commit 5e91243
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 2 deletions.
4 changes: 4 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,17 @@ jobs:
- run: npm install --production
- run: npm install --global .

- name: Link package for yarn on windows
run: yarn link
if: matrix.package-manager == 'yarn' && matrix.os == 'windows'
- name: Create empty package.json for pnpm
run: |
with open("package.json", "w") as f:
f.write('{}')
shell: python
working-directory: ${{ runner.temp }}
if: matrix.package-manager == 'pnpm'

- run: ${{ matrix.package-manager }} exec create-ts-node new-project
working-directory: ${{ runner.temp }}
- run: ${{ matrix.package-manager }} install
Expand Down
17 changes: 15 additions & 2 deletions create.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
import { basename as baseName, join as pathJoin } from "node:path";
import { fileURLToPath } from "node:url";
import { debuglog } from "node:util";
import { EOL } from "os";

const dirname = fileURLToPath(new URL(".", import.meta.url));
const explicitName = process.argv[2];
Expand All @@ -31,6 +32,14 @@ const log = (msg, ...args) => {

const debug = debuglog("create-ts-node");

const lineEndRegex = /\r\n|\r|\n/g;

const normalizeLineEndings = async (path) => {
const file = await readFile(path, { encoding: "utf-8" });
const normalized = file.replaceAll(lineEndRegex, EOL);
await writeFile(path, normalized);
};

const copyFiles = async () => {
const templateFiles = await readDir(templatePath);
for (const file of templateFiles) {
Expand All @@ -45,6 +54,7 @@ const copyFiles = async () => {
templateFiles.push(...newFiles.map((f) => pathJoin(file, f)));
} else if (stats.isFile()) {
await copyFile(templateFilePath, projectFilePath);
await normalizeLineEndings(projectFilePath);
}
}
};
Expand Down Expand Up @@ -72,7 +82,9 @@ const create = async () => {
await copyFiles();

const packageJsonPath = pathJoin(projectPath, "package.json");
const packageJsonFile = await readFile(packageJsonPath);
const packageJsonFile = await readFile(packageJsonPath, {
encoding: "utf-8",
});
const packageJson = JSON.parse(packageJsonFile);

packageJson.name = projectName;
Expand All @@ -82,7 +94,8 @@ const create = async () => {
value.replace("PM_RUN", packageManagerRunScript),
]);

await writeFile(packageJsonPath, JSON.stringify(packageJson, null, 2) + "\n");
await writeFile(packageJsonPath, JSON.stringify(packageJson, null, 2) + EOL);
await normalizeLineEndings(packageJsonPath);

console.log();
`
Expand Down
3 changes: 3 additions & 0 deletions template/.prettierrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"endOfLine": "auto"
}

0 comments on commit 5e91243

Please sign in to comment.