Skip to content

Commit 0f68a65

Browse files
committed
better code organize
1 parent cc62d86 commit 0f68a65

7 files changed

+46
-112
lines changed

Diff for: constants.ts

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
const CONSTANTS = {
2+
METEOR_ROOT_DIRECTORY: "/home/minhna/WORKS/Mike/settler/se2-admin",
3+
};
4+
5+
export default CONSTANTS;

Diff for: transform-export-async-function.ts

+3-49
Original file line numberDiff line numberDiff line change
@@ -7,33 +7,21 @@
77
* 6. Convert them to await expressions
88
*/
99

10-
import fs from "fs";
11-
1210
import {
1311
FileInfo,
1412
API,
1513
Options,
16-
ASTPath,
17-
CallExpression,
1814
ExportDefaultDeclaration,
1915
ExportNamedDeclaration,
20-
AwaitExpression,
21-
Collection,
2216
} from "jscodeshift";
17+
import CONSTANTS from "./constants";
2318

2419
const tsParser = require("jscodeshift/parser/ts");
2520

2621
const debug = require("debug")("transform:export-async-script");
2722
const debug2 = require("debug")("transform:print:export-async-script");
2823

29-
import {
30-
addAwaitKeyword,
31-
convertAllCallExpressionToAsync,
32-
findParentFunction,
33-
setFunctionAsync,
34-
} from "./utils";
35-
36-
const METEOR_ROOT_DIRECTORY = "/home/minhna/WORKS/Mike/settler/se2-admin";
24+
import { convertAllCallExpressionToAsync, getFileContent } from "./utils";
3725

3826
module.exports = function (fileInfo: FileInfo, { j }: API, options: Options) {
3927
debug(
@@ -52,45 +40,11 @@ module.exports = function (fileInfo: FileInfo, { j }: API, options: Options) {
5240

5341
const getRealImportSource = (source: string): string => {
5442
if (/^\//.test(source)) {
55-
return METEOR_ROOT_DIRECTORY + source;
43+
return CONSTANTS.METEOR_ROOT_DIRECTORY + source;
5644
}
5745
return getPathFromSource(fileInfo.path) + "/" + source.replace(/^\.\//, "");
5846
};
5947

60-
const getFileContent = (path: string): string | undefined => {
61-
let fileContent: Buffer | null = null;
62-
63-
if (/(\.js|\.ts)$/.test(path)) {
64-
try {
65-
fileContent = fs.readFileSync(path);
66-
} catch (e) {
67-
debug("File was not found:", path);
68-
}
69-
} else {
70-
try {
71-
fileContent = fs.readFileSync(path + ".js");
72-
} catch (e) {
73-
try {
74-
fileContent = fs.readFileSync(path + ".ts");
75-
} catch (e2) {
76-
// check for index file
77-
try {
78-
fileContent = fs.readFileSync(path + "/index.js");
79-
} catch (e3) {
80-
try {
81-
fileContent = fs.readFileSync(path + "/index.ts");
82-
} catch (e4) {
83-
debug("File was not found");
84-
}
85-
}
86-
}
87-
}
88-
}
89-
90-
// debug("content", fileContent.toString());
91-
return fileContent?.toString();
92-
};
93-
9448
// function to read the export source file, then check for exported async function
9549
interface AnalyzeSourceParams {
9650
exportedFunction: string;

Diff for: transform-find-await-without-async.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
*/
55

66
import { FileInfo, API, Options } from "jscodeshift";
7-
import { findParentFunction, getFunctionLocation } from "./utils";
7+
import { findParentFunction } from "./utils";
88

99
const debug = require("debug")("transform:find-await-without-async");
1010
const debug2 = require("debug")("transform:print:find-await-without-async");

Diff for: transform-fix-async-overly.ts

+1-31
Original file line numberDiff line numberDiff line change
@@ -4,39 +4,9 @@
44
* 3. If there wasn't any await expression, then remove async from function expression.
55
*/
66

7-
import {
8-
FileInfo,
9-
API,
10-
Options,
11-
ASTPath,
12-
Collection,
13-
MemberExpression,
14-
CallExpression,
15-
ExpressionStatement,
16-
FunctionDeclaration,
17-
ArrowFunctionExpression,
18-
FunctionExpression,
19-
Position,
20-
} from "jscodeshift";
21-
22-
const methodsMapping = {
23-
findOne: "findOneAsync",
24-
insert: "insertAsync",
25-
upsert: "upsertAsync",
26-
update: "updateAsync",
27-
remove: "removeAsync",
28-
createIndex: "createIndexAsync",
29-
dropIndex: "dropIndexAsync",
30-
dropCollection: "dropCollectionAsync",
31-
// methods on cursors
32-
count: "countAsync",
33-
fetch: "fetchAsync",
34-
forEach: "forEachAsync",
35-
map: "mapAsync",
36-
};
7+
import { FileInfo, API, Options, ASTPath } from "jscodeshift";
378

389
import {
39-
addAwaitKeyword,
4010
findParentFunction,
4111
getFunctionLocation,
4212
setFunctionNotAsync,

Diff for: transform-rename-functions.ts

-13
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,8 @@ import {
99
API,
1010
Options,
1111
ASTPath,
12-
Collection,
13-
MemberExpression,
1412
CallExpression,
1513
ExpressionStatement,
16-
FunctionDeclaration,
17-
ArrowFunctionExpression,
18-
FunctionExpression,
1914
} from "jscodeshift";
2015

2116
import * as recast from "recast";
@@ -26,14 +21,6 @@ import {
2621
setFunctionAsync,
2722
} from "./utils";
2823

29-
type MethodDescType = {
30-
type: string;
31-
objectName?: string;
32-
propertyName?: string;
33-
functionName?: string;
34-
await?: boolean;
35-
};
36-
3724
type MethodsMappingItemType = {
3825
from: string;
3926
to: string;

Diff for: transform-use-async-function.ts

+1-18
Original file line numberDiff line numberDiff line change
@@ -7,34 +7,17 @@
77
import fs from "fs";
88
6;
99

10-
import {
11-
FileInfo,
12-
API,
13-
Options,
14-
ASTPath,
15-
CallExpression,
16-
ExportDefaultDeclaration,
17-
ExportNamedDeclaration,
18-
AwaitExpression,
19-
Collection,
20-
} from "jscodeshift";
21-
22-
const tsParser = require("jscodeshift/parser/ts");
10+
import { FileInfo, API, Options } from "jscodeshift";
2311

2412
const debug = require("debug")("transform:use-async-function");
2513
const debug2 = require("debug")("transform:print:use-async-function");
2614

2715
import {
28-
addAwaitKeyword,
2916
convertAllCallExpressionToAsync,
3017
convertAllMemberExpressionCallToAsync,
31-
findParentFunction,
3218
findParentObject,
33-
setFunctionAsync,
3419
} from "./utils";
3520

36-
const METEOR_ROOT_DIRECTORY = "/home/minhna/WORKS/Mike/settler/se2-admin";
37-
3821
module.exports = function (fileInfo: FileInfo, { j }: API, options: Options) {
3922
debug(
4023
`\n**************************************************

Diff for: utils.ts

+35
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { ASTPath, CallExpression, JSCodeshift, Collection } from "jscodeshift";
2+
import fs from "fs";
23

34
const debug = require("debug")("transform:utils");
45

@@ -185,3 +186,37 @@ export const getFunctionLocation = (p: ASTPath) => {
185186
debug("Unhandled function type:", p.value.type);
186187
}
187188
};
189+
190+
export const getFileContent = (path: string): string | undefined => {
191+
let fileContent: Buffer | null = null;
192+
193+
if (/(\.js|\.ts)$/.test(path)) {
194+
try {
195+
fileContent = fs.readFileSync(path);
196+
} catch (e) {
197+
debug("File was not found:", path);
198+
}
199+
} else {
200+
try {
201+
fileContent = fs.readFileSync(path + ".js");
202+
} catch (e) {
203+
try {
204+
fileContent = fs.readFileSync(path + ".ts");
205+
} catch (e2) {
206+
// check for index file
207+
try {
208+
fileContent = fs.readFileSync(path + "/index.js");
209+
} catch (e3) {
210+
try {
211+
fileContent = fs.readFileSync(path + "/index.ts");
212+
} catch (e4) {
213+
debug("File was not found");
214+
}
215+
}
216+
}
217+
}
218+
}
219+
220+
// debug("content", fileContent.toString());
221+
return fileContent?.toString();
222+
};

0 commit comments

Comments
 (0)