-
-
Notifications
You must be signed in to change notification settings - Fork 528
Expand file tree
/
Copy pathpath-helper.js
More file actions
110 lines (91 loc) · 2.31 KB
/
path-helper.js
File metadata and controls
110 lines (91 loc) · 2.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
import path from 'path';
import fs from 'fs';
import process from 'process';
import { isTypescriptProject } from './import-helper';
const resolve = require('resolve').sync;
import getYArgs from '../core/yargs';
const args = getYArgs().argv;
function format(i) {
return parseInt(i, 10) < 10 ? '0' + i : i;
}
function getCurrentYYYYMMDDHHmms() {
const date = new Date();
return [
date.getUTCFullYear(),
format(date.getUTCMonth() + 1),
format(date.getUTCDate()),
format(date.getUTCHours()),
format(date.getUTCMinutes()),
format(date.getUTCSeconds()),
].join('');
}
module.exports = {
getPath(type) {
type = type + 's';
let result = args[type + 'Path'] || path.resolve(process.cwd(), type);
if (path.normalize(result) !== path.resolve(result)) {
// the path is relative
result = path.resolve(process.cwd(), result);
}
return result;
},
getFileName(type, name, options) {
return this.addFileExtension(
[getCurrentYYYYMMDDHHmms(), name ? name : 'unnamed-' + type].join('-'),
options
);
},
getFileExtension() {
return isTypescriptProject ? 'ts' : 'js';
},
addFileExtension(basename, options) {
return [basename, this.getFileExtension(options)].join('.');
},
getMigrationPath(migrationName) {
return path.resolve(
this.getPath('migration'),
this.getFileName('migration', migrationName)
);
},
getSeederPath(seederName) {
return path.resolve(
this.getPath('seeder'),
this.getFileName('seeder', seederName)
);
},
getModelsPath() {
return args.modelsPath || path.resolve(process.cwd(), 'models');
},
getModelPath(modelName) {
return path.resolve(
this.getModelsPath(),
this.addFileExtension(modelName.toLowerCase())
);
},
resolve(packageName) {
let result;
try {
result = resolve(packageName, { basedir: process.cwd() });
result = require(result);
} catch (e) {
try {
result = require(packageName);
} catch (err) {
// ignore error
}
}
return result;
},
existsSync(pathToCheck) {
if (fs.accessSync) {
try {
fs.accessSync(pathToCheck, fs.R_OK);
return true;
} catch (e) {
return false;
}
} else {
return fs.existsSync(pathToCheck);
}
},
};