forked from canada-ca/tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupgrade-at.js
More file actions
119 lines (102 loc) · 3.09 KB
/
upgrade-at.js
File metadata and controls
119 lines (102 loc) · 3.09 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
111
112
113
114
115
116
117
118
119
const j = require('jscodeshift')
const path = require('path')
export default (file, _api, { schemaPath }) => {
const cwd = path.parse(path.resolve(file.path)).dir
const pathToSchema = path.relative(path.join(cwd), path.join(schemaPath))
const schemaImport = j.importDeclaration(
[j.importDefaultSpecifier(j.identifier('dbschema'))],
j.literal(pathToSchema),
)
const ast = j(file.source)
const imports = ast.find(j.ImportDeclaration)
function appendToImports(thing) {
if (imports.length) {
return j(imports.at(imports.length - 1).get()).insertAfter(thing)
}
}
const functions = ast.find(j.CallExpression, {
callee: { type: 'Identifier', name: 'ensure' },
})
// ensure function found
if (functions.length) {
// is there an import for schema already?
const si = ast.find(j.ImportDefaultSpecifier, {
type: 'ImportDefaultSpecifier',
local: { type: 'Identifier', name: 'dbschema' },
})
console.log({ length: si.size(), si: si.nodes() })
if (si.size() == 0) {
appendToImports(schemaImport)
}
// delete the old databaseOptions import
ast
.find(j.ImportDeclaration, {
type: 'ImportDeclaration',
specifiers: [
{
type: 'ImportSpecifier',
local: { type: 'Identifier', name: 'databaseOptions' },
},
],
})
.remove()
// replace the old args with the new args
functions.replaceWith((nodePath) => {
const { node } = nodePath
const argsObject = object({
properties: [
property({
key: 'variables',
value: object({
properties: [
property({
key: 'dbname',
value: functionCall({
name: 'dbNameFromFile',
args: [variable('__filename')],
}),
}),
property({ key: 'username', value: string('root') }),
property({ key: 'rootPassword', value: variable('rootPass') }),
property({ key: 'password', value: variable('rootPass') }),
shorthandProperty('url'),
],
}),
}),
property({ key: 'schema', value: variable('dbschema') }),
],
})
node.arguments = [argsObject]
return node
})
return ast.toSource({
trailingComma: true,
arrowParensAlways: true,
quote: 'single',
})
}
}
function property({ key = 'username', value = 'root' }) {
if (typeof value == 'string') {
return j.objectProperty(j.identifier(key), j.literal(value))
} else {
return j.objectProperty(j.identifier(key), value)
}
}
function functionCall({ name, args }) {
return j.callExpression(j.identifier(name), args)
}
function shorthandProperty(name) {
const prop = j.objectProperty(j.identifier(name), j.identifier(name))
prop.shorthand = true
return prop
}
function string(str) {
return j.literal(str)
}
function variable(name) {
return j.identifier(name)
}
function object({ properties }) {
return j.objectExpression(properties)
}