Skip to content

Commit

Permalink
fix: fix json parse
Browse files Browse the repository at this point in the history
  • Loading branch information
jiangweiye committed Aug 1, 2024
1 parent c07dd75 commit 23a81a6
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 27 deletions.
29 changes: 18 additions & 11 deletions dist/index.cjs.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ __export(src_exports, {
});
module.exports = __toCommonJS(src_exports);
var import_node_path3 = require("path");
var import_node_process2 = __toESM(require("process"), 1);
var import_node_process = __toESM(require("process"), 1);

// src/shared.ts
var import_node_fs = require("fs");
Expand All @@ -57,17 +57,24 @@ var getDirs = /* @__PURE__ */ __name((path) => {
return result;
}, []);
}, "getDirs");
var removeComments = /* @__PURE__ */ __name((code) => {
return code.replace(/\/\/.*|\/\*[\s\S]*?\*\//g, "");
}, "removeComments");

// src/sync.ts
var import_node_fs2 = require("fs");
var import_node_path2 = require("path");
var import_node_process = __toESM(require("process"), 1);
function getJson(jsonPath) {
try {
const jsonText = (0, import_node_fs2.readFileSync)(jsonPath, "utf-8");
let jsonText = (0, import_node_fs2.readFileSync)(jsonPath, "utf-8");
jsonText = removeComments(jsonText);
return JSON.parse(jsonText);
} catch (error) {
import_node_process.default.exit(0);
return {
compilerOptions: {
paths: {}
}
};
}
}
__name(getJson, "getJson");
Expand Down Expand Up @@ -167,7 +174,7 @@ __name(removeJson, "removeJson");

// src/index.ts
var DEFAULT_CONFIG = {
root: (0, import_node_path3.join)(import_node_process2.default.cwd(), "src"),
root: (0, import_node_path3.join)(import_node_process.default.cwd(), "src"),
prefix: "@",
mode: "sync",
aliasPath: null
Expand Down Expand Up @@ -211,8 +218,8 @@ var src_default = /* @__PURE__ */ __name((options = DEFAULT_CONFIG) => {
const alias = genArrayAlias(dirs, root, prefix);
syncJson({
aliasPath,
jsJson: jsconfig(import_node_process2.default.cwd()),
tsJson: tsconfig(import_node_process2.default.cwd()),
jsJson: jsconfig(import_node_process.default.cwd()),
tsJson: tsconfig(import_node_process.default.cwd()),
alias,
root,
prefix,
Expand All @@ -232,8 +239,8 @@ var src_default = /* @__PURE__ */ __name((options = DEFAULT_CONFIG) => {
const alias = genArrayAlias(dirs, root, prefix);
mode !== "off" && syncJson({
aliasPath,
jsJson: jsconfig(import_node_process2.default.cwd()),
tsJson: tsconfig(import_node_process2.default.cwd()),
jsJson: jsconfig(import_node_process.default.cwd()),
tsJson: tsconfig(import_node_process.default.cwd()),
alias,
root,
prefix,
Expand All @@ -244,8 +251,8 @@ var src_default = /* @__PURE__ */ __name((options = DEFAULT_CONFIG) => {
if (eventName === "unlinkDir") {
mode !== "off" && removeJson({
aliasPath,
jsJson: jsconfig(import_node_process2.default.cwd()),
tsJson: tsconfig(import_node_process2.default.cwd()),
jsJson: jsconfig(import_node_process.default.cwd()),
tsJson: tsconfig(import_node_process.default.cwd()),
unlinkDirName,
root,
prefix,
Expand Down
29 changes: 18 additions & 11 deletions dist/index.esm.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ var __name = (target, value) => __defProp(target, "name", { value, configurable:

// src/index.ts
import { join as join2, parse as parse2 } from "path";
import process2 from "process";
import process from "process";

// src/shared.ts
import { existsSync, lstatSync, readdirSync } from "fs";
Expand All @@ -26,17 +26,24 @@ var getDirs = /* @__PURE__ */ __name((path) => {
return result;
}, []);
}, "getDirs");
var removeComments = /* @__PURE__ */ __name((code) => {
return code.replace(/\/\/.*|\/\*[\s\S]*?\*\//g, "");
}, "removeComments");

// src/sync.ts
import { readFileSync, writeFileSync } from "fs";
import { parse } from "path";
import process from "process";
function getJson(jsonPath) {
try {
const jsonText = readFileSync(jsonPath, "utf-8");
let jsonText = readFileSync(jsonPath, "utf-8");
jsonText = removeComments(jsonText);
return JSON.parse(jsonText);
} catch (error) {
process.exit(0);
return {
compilerOptions: {
paths: {}
}
};
}
}
__name(getJson, "getJson");
Expand Down Expand Up @@ -136,7 +143,7 @@ __name(removeJson, "removeJson");

// src/index.ts
var DEFAULT_CONFIG = {
root: join2(process2.cwd(), "src"),
root: join2(process.cwd(), "src"),
prefix: "@",
mode: "sync",
aliasPath: null
Expand Down Expand Up @@ -180,8 +187,8 @@ var src_default = /* @__PURE__ */ __name((options = DEFAULT_CONFIG) => {
const alias = genArrayAlias(dirs, root, prefix);
syncJson({
aliasPath,
jsJson: jsconfig(process2.cwd()),
tsJson: tsconfig(process2.cwd()),
jsJson: jsconfig(process.cwd()),
tsJson: tsconfig(process.cwd()),
alias,
root,
prefix,
Expand All @@ -201,8 +208,8 @@ var src_default = /* @__PURE__ */ __name((options = DEFAULT_CONFIG) => {
const alias = genArrayAlias(dirs, root, prefix);
mode !== "off" && syncJson({
aliasPath,
jsJson: jsconfig(process2.cwd()),
tsJson: tsconfig(process2.cwd()),
jsJson: jsconfig(process.cwd()),
tsJson: tsconfig(process.cwd()),
alias,
root,
prefix,
Expand All @@ -213,8 +220,8 @@ var src_default = /* @__PURE__ */ __name((options = DEFAULT_CONFIG) => {
if (eventName === "unlinkDir") {
mode !== "off" && removeJson({
aliasPath,
jsJson: jsconfig(process2.cwd()),
tsJson: tsconfig(process2.cwd()),
jsJson: jsconfig(process.cwd()),
tsJson: tsconfig(process.cwd()),
unlinkDirName,
root,
prefix,
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "vite-plugin-auto-alias",
"type": "module",
"version": "1.0.6",
"version": "1.0.7",
"description": "automatically generate alias based on path",
"author": "jiangweiye <[email protected]> (https://github.com/jwyGithub)",
"license": "MIT",
Expand Down
4 changes: 4 additions & 0 deletions src/shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,7 @@ export const getDirs = (path: string): GetDirs => {
return result;
}, []);
};

export const removeComments = (code: string) => {
return code.replace(/\/\/.*|\/\*[\s\S]*?\*\//g, '');
};
12 changes: 8 additions & 4 deletions src/sync.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { readFileSync, writeFileSync } from 'node:fs';
import { parse } from 'node:path';
import process from 'node:process';
import type { Alias } from 'vite';
import { hasFile } from './shared';
import { hasFile, removeComments } from './shared';
import type { IJson, IPaths, Mode } from './type';

/**
Expand All @@ -12,10 +11,15 @@ import type { IJson, IPaths, Mode } from './type';
*/
export function getJson(jsonPath: string): IJson {
try {
const jsonText = readFileSync(jsonPath, 'utf-8');
let jsonText = readFileSync(jsonPath, 'utf-8');
jsonText = removeComments(jsonText);
return JSON.parse(jsonText);
} catch (error) {
process.exit(0);
return {
compilerOptions: {
paths: {}
}
};
}
}
/**
Expand Down

0 comments on commit 23a81a6

Please sign in to comment.