@prisma-psm/core 1.0.55 → 1.0.57
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/launcher/commands/shell.d.ts +5 -0
- package/src/launcher/commands/shell.d.ts.map +1 -0
- package/src/launcher/commands/shell.js +32 -0
- package/src/launcher/commands/shell.js.map +1 -0
- package/src/launcher/commands/shell.ts +34 -0
- package/src/tools/deploy.d.ts.map +1 -1
- package/src/tools/deploy.js +11 -7
- package/src/tools/deploy.js.map +1 -1
- package/src/tools/deploy.ts +53 -46
- package/src/tools/shell.d.ts +8 -0
- package/src/tools/shell.d.ts.map +1 -0
- package/src/tools/shell.js +221 -0
- package/src/tools/shell.js.map +1 -0
- package/src/tools/shell.ts +210 -0
package/package.json
CHANGED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"shell.d.ts","sourceRoot":"","sources":["shell.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,aAAa,EAAE,MAAM,OAAO,CAAC;AACtC,OAAO,EAAS,YAAY,EAAE,MAAM,mBAAmB,CAAC;AAExD,QAAA,MAAM,OAAO,EAAE,aAAa,CAAC,YAAY,EAAE,YAAY,CA0BtD,CAAC;AAEF,SAAS,OAAO,CAAC"}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
// filename: src/launcher/commands/shell.ts
|
|
3
|
+
const shell_1 = require("../../tools/shell");
|
|
4
|
+
const command = {
|
|
5
|
+
command: "shell",
|
|
6
|
+
describe: "Open interactive SQL shell with transaction (similar to psql)",
|
|
7
|
+
builder: args => {
|
|
8
|
+
return args
|
|
9
|
+
.options("schema", {
|
|
10
|
+
type: "string",
|
|
11
|
+
alias: "s",
|
|
12
|
+
describe: "Caminho para o schema.prisma (opcional)",
|
|
13
|
+
})
|
|
14
|
+
.options("label", {
|
|
15
|
+
type: "string",
|
|
16
|
+
alias: "l",
|
|
17
|
+
describe: "Label para a revisão gerada ao commitar",
|
|
18
|
+
})
|
|
19
|
+
.options("add", {
|
|
20
|
+
type: "boolean",
|
|
21
|
+
describe: "Adicionar o arquivo .tar.gz ao git após gerar revisão",
|
|
22
|
+
});
|
|
23
|
+
},
|
|
24
|
+
handler: (argv) => {
|
|
25
|
+
(0, shell_1.shell)(argv).catch((reason) => {
|
|
26
|
+
console.error("Erro no shell:", reason);
|
|
27
|
+
process.exit(1);
|
|
28
|
+
});
|
|
29
|
+
},
|
|
30
|
+
};
|
|
31
|
+
module.exports = command;
|
|
32
|
+
//# sourceMappingURL=shell.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"shell.js","sourceRoot":"","sources":["shell.ts"],"names":[],"mappings":";AAAA,2CAA2C;AAG3C,6CAAwD;AAExD,MAAM,OAAO,GAA8C;IACvD,OAAO,EAAE,OAAO;IAChB,QAAQ,EAAE,+DAA+D;IACzE,OAAO,EAAE,IAAI,CAAC,EAAE;QACZ,OAAO,IAAI;aACN,OAAO,CAAC,QAAQ,EAAE;YACf,IAAI,EAAE,QAAQ;YACd,KAAK,EAAE,GAAG;YACV,QAAQ,EAAE,yCAAyC;SACtD,CAAC;aACD,OAAO,CAAC,OAAO,EAAE;YACd,IAAI,EAAE,QAAQ;YACd,KAAK,EAAE,GAAG;YACV,QAAQ,EAAE,yCAAyC;SACtD,CAAC;aACD,OAAO,CAAC,KAAK,EAAE;YACZ,IAAI,EAAE,SAAS;YACf,QAAQ,EAAE,uDAAuD;SACpE,CAAC,CAAC;IACX,CAAC;IACD,OAAO,EAAE,CAAC,IAAI,EAAE,EAAE;QACd,IAAA,aAAK,EAAC,IAAI,CAAC,CAAC,KAAK,CAAC,CAAC,MAAM,EAAE,EAAE;YACzB,OAAO,CAAC,KAAK,CAAC,gBAAgB,EAAE,MAAM,CAAC,CAAC;YACxC,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;QACpB,CAAC,CAAC,CAAC;IACP,CAAC;CACJ,CAAC;AAEF,iBAAS,OAAO,CAAC"}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
// filename: src/launcher/commands/shell.ts
|
|
2
|
+
|
|
3
|
+
import { CommandModule } from "yargs";
|
|
4
|
+
import { shell, ShellOptions } from "../../tools/shell";
|
|
5
|
+
|
|
6
|
+
const command: CommandModule<ShellOptions, ShellOptions> = {
|
|
7
|
+
command: "shell",
|
|
8
|
+
describe: "Open interactive SQL shell with transaction (similar to psql)",
|
|
9
|
+
builder: args => {
|
|
10
|
+
return args
|
|
11
|
+
.options("schema", {
|
|
12
|
+
type: "string",
|
|
13
|
+
alias: "s",
|
|
14
|
+
describe: "Caminho para o schema.prisma (opcional)",
|
|
15
|
+
})
|
|
16
|
+
.options("label", {
|
|
17
|
+
type: "string",
|
|
18
|
+
alias: "l",
|
|
19
|
+
describe: "Label para a revisão gerada ao commitar",
|
|
20
|
+
})
|
|
21
|
+
.options("add", {
|
|
22
|
+
type: "boolean",
|
|
23
|
+
describe: "Adicionar o arquivo .tar.gz ao git após gerar revisão",
|
|
24
|
+
});
|
|
25
|
+
},
|
|
26
|
+
handler: (argv) => {
|
|
27
|
+
shell(argv).catch((reason) => {
|
|
28
|
+
console.error("Erro no shell:", reason);
|
|
29
|
+
process.exit(1);
|
|
30
|
+
});
|
|
31
|
+
},
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
export = command;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"deploy.d.ts","sourceRoot":"","sources":["deploy.ts"],"names":[],"mappings":"AAOA,OAAO,
|
|
1
|
+
{"version":3,"file":"deploy.d.ts","sourceRoot":"","sources":["deploy.ts"],"names":[],"mappings":"AAOA,OAAO,EAAE,aAAa,EAAE,MAAM,YAAY,CAAC;AAE3C,OAAO,EAAE,SAAS,EAAE,MAAM,WAAW,CAAC;AAGtC,MAAM,WAAW,aAAa;IAC1B,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,kBAAkB,EAAE,MAAM,CAAC;CAC9B;AAID,wBAAsB,MAAM,CAAC,IAAI,EAAE,aAAa,iBA0E/C;AAED,MAAM,WAAW,YAAY;IACzB,IAAI,EAAE,MAAM,CAAC;IACb,MAAM,EAAE,SAAS,CAAC;IAClB,GAAG,EAAE,aAAa,CAAC;CACtB;AAED,wBAAsB,KAAK,CAAC,IAAI,EAAE,YAAY;;cAKhC,MAAM;aACP,aAAa;iBACT,MAAM;iBACN,MAAM;gBACP,OAAO;eACR,MAAM;iBACJ,MAAM,EAAE;cACX,IAAI,GAAG,IAAI;;;;;;cAPX,MAAM;aACP,aAAa;iBACT,MAAM;iBACN,MAAM;gBACP,OAAO;eACR,MAAM;iBACJ,MAAM,EAAE;cACX,IAAI,GAAG,IAAI;;;;;;cAPX,MAAM;aACP,aAAa;iBACT,MAAM;iBACN,MAAM;gBACP,OAAO;eACR,MAAM;iBACJ,MAAM,EAAE;cACX,IAAI,GAAG,IAAI;;;;GAuFxB"}
|
package/src/tools/deploy.js
CHANGED
|
@@ -68,7 +68,7 @@ function deploy(opts) {
|
|
|
68
68
|
const pullResponse = yield fetch({
|
|
69
69
|
home: home,
|
|
70
70
|
driver: driver,
|
|
71
|
-
psm: psm
|
|
71
|
+
psm: psm,
|
|
72
72
|
});
|
|
73
73
|
if (pullResponse.error) {
|
|
74
74
|
pullResponse.clean();
|
|
@@ -78,7 +78,9 @@ function deploy(opts) {
|
|
|
78
78
|
pullResponse.clean();
|
|
79
79
|
throw new Error(`No migrate commited! Use ${chalk_1.default.bold("psm migrate commit")} first!`);
|
|
80
80
|
}
|
|
81
|
-
|
|
81
|
+
let revs = pullResponse.revs;
|
|
82
|
+
// Ordena as revisões por instante (cronologicamente)
|
|
83
|
+
revs.sort((a, b) => a.psm.migration.instante.localeCompare(b.psm.migration.instante));
|
|
82
84
|
for (let i = 0; i < revs.length; i++) {
|
|
83
85
|
let next = revs[i];
|
|
84
86
|
const migrator = driver.migrator({
|
|
@@ -88,9 +90,11 @@ function deploy(opts) {
|
|
|
88
90
|
core: "",
|
|
89
91
|
});
|
|
90
92
|
next.message.forEach(console.log);
|
|
93
|
+
// CORREÇÃO: se já foi migrada, apenas continua para a próxima
|
|
91
94
|
if (next.pulled)
|
|
92
|
-
|
|
93
|
-
|
|
95
|
+
continue;
|
|
96
|
+
// Restaura backup apenas na primeira revisão não aplicada (se houver)
|
|
97
|
+
if (i === 0 && !!next.backup) {
|
|
94
98
|
yield migrator.restore(next.backup);
|
|
95
99
|
}
|
|
96
100
|
const result = yield migrator.migrate();
|
|
@@ -138,7 +142,7 @@ function fetch(opts) {
|
|
|
138
142
|
message: [],
|
|
139
143
|
date: null,
|
|
140
144
|
backup: backup,
|
|
141
|
-
temp: tempDir
|
|
145
|
+
temp: tempDir,
|
|
142
146
|
};
|
|
143
147
|
if (fs.existsSync(backup))
|
|
144
148
|
rev.backup = backup;
|
|
@@ -154,7 +158,7 @@ function fetch(opts) {
|
|
|
154
158
|
return {
|
|
155
159
|
revs,
|
|
156
160
|
error: new Error(`MISSING PREVIEW migration for...\n${missed.join(", ")}`),
|
|
157
|
-
clean() { }
|
|
161
|
+
clean() { },
|
|
158
162
|
};
|
|
159
163
|
}
|
|
160
164
|
// marcar revisões já migradas
|
|
@@ -183,7 +187,7 @@ function fetch(opts) {
|
|
|
183
187
|
revs.forEach(value => {
|
|
184
188
|
fs.rmSync(value.temp, { recursive: true, force: true });
|
|
185
189
|
});
|
|
186
|
-
}
|
|
190
|
+
},
|
|
187
191
|
};
|
|
188
192
|
});
|
|
189
193
|
}
|
package/src/tools/deploy.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"deploy.js","sourceRoot":"","sources":["deploy.ts"],"names":[],"mappings":";AAAA,gCAAgC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAqBhC,
|
|
1
|
+
{"version":3,"file":"deploy.js","sourceRoot":"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,GAAG,CAAC,IAAI,EAAE,CAAC,CAAC;gBAC9D,IAAI,CAAC,MAAM,GAAG,IAAI,CAAC;YACvB,CAAC;QACL,CAAC;QAED,OAAO;YACH,IAAI;YACJ,KAAK;gBACD,IAAI,CAAC,OAAO,CAAC,KAAK,CAAC,EAAE;oBACjB,EAAE,CAAC,MAAM,CAAC,KAAK,CAAC,IAAI,EAAE,EAAE,SAAS,EAAE,IAAI,EAAE,KAAK,EAAE,IAAI,EAAE,CAAC,CAAC;gBAC5D,CAAC,CAAC,CAAC;YACP,CAAC;SACJ,CAAC;IACN,CAAC;CAAA"}
|
package/src/tools/deploy.ts
CHANGED
|
@@ -5,58 +5,61 @@ import * as os from "node:os";
|
|
|
5
5
|
import * as fs from "node:fs";
|
|
6
6
|
import * as yaml from "yaml";
|
|
7
7
|
import * as Path from "node:path";
|
|
8
|
-
import {PSMConfigFile} from "../configs";
|
|
8
|
+
import { PSMConfigFile } from "../configs";
|
|
9
9
|
import chalk from "chalk";
|
|
10
|
-
import {PSMDriver} from "../driver";
|
|
11
|
-
import {psmLockup} from "./common";
|
|
10
|
+
import { PSMDriver } from "../driver";
|
|
11
|
+
import { psmLockup } from "./common";
|
|
12
12
|
|
|
13
13
|
export interface DeployOptions {
|
|
14
|
-
schema?:string
|
|
15
|
-
generate?:string
|
|
16
|
-
label?:string
|
|
17
|
-
"generate-command":string
|
|
14
|
+
schema?: string;
|
|
15
|
+
generate?: string;
|
|
16
|
+
label?: string;
|
|
17
|
+
"generate-command": string;
|
|
18
18
|
}
|
|
19
19
|
|
|
20
|
-
const TAG = "PSM DEPLOY >"
|
|
20
|
+
const TAG = "PSM DEPLOY >";
|
|
21
21
|
|
|
22
|
-
export async function deploy(
|
|
22
|
+
export async function deploy(opts: DeployOptions) {
|
|
23
23
|
require('dotenv').config();
|
|
24
24
|
|
|
25
|
-
const { psm, psm_sql, driver, home} = await psmLockup({ schema: opts.schema });
|
|
25
|
+
const { psm, psm_sql, driver, home } = await psmLockup({ schema: opts.schema });
|
|
26
26
|
|
|
27
27
|
let migrator = driver.migrator({
|
|
28
28
|
url: process.env[psm.psm.url],
|
|
29
29
|
migrate: "",
|
|
30
30
|
check: "",
|
|
31
|
-
core: fs.readFileSync(
|
|
31
|
+
core: fs.readFileSync(psm_sql).toString(),
|
|
32
32
|
});
|
|
33
33
|
|
|
34
34
|
let result = await migrator.core();
|
|
35
|
-
if(
|
|
36
|
-
console.error(
|
|
37
|
-
result.messages.forEach(
|
|
38
|
-
console.error(
|
|
35
|
+
if (!result.success) {
|
|
36
|
+
console.error(result.error);
|
|
37
|
+
result.messages.forEach(error => {
|
|
38
|
+
console.error(error);
|
|
39
39
|
});
|
|
40
|
-
throw new Error(
|
|
40
|
+
throw new Error("Migrate error: Core failed!");
|
|
41
41
|
}
|
|
42
42
|
|
|
43
43
|
const pullResponse = await fetch({
|
|
44
44
|
home: home,
|
|
45
45
|
driver: driver,
|
|
46
|
-
psm: psm
|
|
46
|
+
psm: psm,
|
|
47
47
|
});
|
|
48
48
|
|
|
49
|
-
if(
|
|
49
|
+
if (pullResponse.error) {
|
|
50
50
|
pullResponse.clean();
|
|
51
|
-
throw
|
|
51
|
+
throw pullResponse.error;
|
|
52
52
|
}
|
|
53
53
|
|
|
54
|
-
if(
|
|
54
|
+
if (!pullResponse.revs?.length) {
|
|
55
55
|
pullResponse.clean();
|
|
56
|
-
throw new Error(`No migrate commited! Use ${
|
|
56
|
+
throw new Error(`No migrate commited! Use ${chalk.bold("psm migrate commit")} first!`);
|
|
57
57
|
}
|
|
58
58
|
|
|
59
|
-
|
|
59
|
+
let revs = pullResponse.revs;
|
|
60
|
+
|
|
61
|
+
// Ordena as revisões por instante (cronologicamente)
|
|
62
|
+
revs.sort((a, b) => a.psm.migration.instante.localeCompare(b.psm.migration.instante));
|
|
60
63
|
|
|
61
64
|
for (let i = 0; i < revs.length; i++) {
|
|
62
65
|
let next = revs[i];
|
|
@@ -66,40 +69,44 @@ export async function deploy( opts: DeployOptions) {
|
|
|
66
69
|
check: "",
|
|
67
70
|
core: "",
|
|
68
71
|
});
|
|
69
|
-
next.message.forEach(
|
|
72
|
+
next.message.forEach(console.log);
|
|
73
|
+
|
|
74
|
+
// CORREÇÃO: se já foi migrada, apenas continua para a próxima
|
|
75
|
+
if (next.pulled) continue;
|
|
70
76
|
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
await migrator.restore(
|
|
77
|
+
// Restaura backup apenas na primeira revisão não aplicada (se houver)
|
|
78
|
+
if (i === 0 && !!next.backup) {
|
|
79
|
+
await migrator.restore(next.backup);
|
|
74
80
|
}
|
|
75
81
|
|
|
76
82
|
const result = await migrator.migrate();
|
|
77
83
|
|
|
78
|
-
if(
|
|
79
|
-
console.error(
|
|
80
|
-
result.messages.forEach(
|
|
81
|
-
console.error(
|
|
84
|
+
if (!result.success) {
|
|
85
|
+
console.error(result.error);
|
|
86
|
+
result.messages.forEach(error => {
|
|
87
|
+
console.error(error);
|
|
82
88
|
});
|
|
83
|
-
console.error(
|
|
84
|
-
throw new Error(
|
|
89
|
+
console.error(`${next.label} is migrated at ${next.date} - ${chalk.redBright.bold("FAILED")}`);
|
|
90
|
+
throw new Error("Migrate error: Push migration failed!");
|
|
85
91
|
}
|
|
86
|
-
console.log(
|
|
92
|
+
console.log(`${next.label} is migrated at ${next.date} - ${chalk.greenBright.bold("SUCCESS")}`);
|
|
87
93
|
}
|
|
88
94
|
|
|
89
95
|
pullResponse.clean();
|
|
90
96
|
}
|
|
91
97
|
|
|
92
98
|
export interface FetchOptions {
|
|
93
|
-
home:string
|
|
94
|
-
driver:PSMDriver
|
|
95
|
-
psm:PSMConfigFile
|
|
99
|
+
home: string;
|
|
100
|
+
driver: PSMDriver;
|
|
101
|
+
psm: PSMConfigFile;
|
|
96
102
|
}
|
|
103
|
+
|
|
97
104
|
export async function fetch(opts: FetchOptions) {
|
|
98
105
|
const revisionsDir = Path.join(opts.home, "psm/revisions/schema");
|
|
99
106
|
const revFiles = fs.readdirSync(revisionsDir).filter(n => n.endsWith(".tar.gz"));
|
|
100
107
|
|
|
101
108
|
const revs: Array<{
|
|
102
|
-
temp:string
|
|
109
|
+
temp: string;
|
|
103
110
|
psm: PSMConfigFile;
|
|
104
111
|
migrate: string;
|
|
105
112
|
backup?: string;
|
|
@@ -132,7 +139,7 @@ export async function fetch(opts: FetchOptions) {
|
|
|
132
139
|
const psm = yaml.parse(fs.readFileSync(psmPath, "utf-8")) as PSMConfigFile;
|
|
133
140
|
const migrate = fs.readFileSync(migrationPath, "utf-8");
|
|
134
141
|
|
|
135
|
-
const rev:typeof revs[number] = {
|
|
142
|
+
const rev: typeof revs[number] = {
|
|
136
143
|
psm,
|
|
137
144
|
migrate,
|
|
138
145
|
pulled: false,
|
|
@@ -140,10 +147,10 @@ export async function fetch(opts: FetchOptions) {
|
|
|
140
147
|
message: [],
|
|
141
148
|
date: null,
|
|
142
149
|
backup: backup,
|
|
143
|
-
temp: tempDir
|
|
144
|
-
}
|
|
150
|
+
temp: tempDir,
|
|
151
|
+
};
|
|
145
152
|
|
|
146
|
-
if(
|
|
153
|
+
if (fs.existsSync(backup)) rev.backup = backup;
|
|
147
154
|
revs.push(rev);
|
|
148
155
|
}
|
|
149
156
|
|
|
@@ -158,7 +165,7 @@ export async function fetch(opts: FetchOptions) {
|
|
|
158
165
|
return {
|
|
159
166
|
revs,
|
|
160
167
|
error: new Error(`MISSING PREVIEW migration for...\n${missed.join(", ")}`),
|
|
161
|
-
clean(){}
|
|
168
|
+
clean() {},
|
|
162
169
|
};
|
|
163
170
|
}
|
|
164
171
|
|
|
@@ -187,10 +194,10 @@ export async function fetch(opts: FetchOptions) {
|
|
|
187
194
|
|
|
188
195
|
return {
|
|
189
196
|
revs,
|
|
190
|
-
clean(){
|
|
191
|
-
revs.forEach(
|
|
197
|
+
clean() {
|
|
198
|
+
revs.forEach(value => {
|
|
192
199
|
fs.rmSync(value.temp, { recursive: true, force: true });
|
|
193
|
-
})
|
|
194
|
-
}
|
|
200
|
+
});
|
|
201
|
+
},
|
|
195
202
|
};
|
|
196
203
|
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"shell.d.ts","sourceRoot":"","sources":["shell.ts"],"names":[],"mappings":"AAcA,MAAM,WAAW,YAAY;IACzB,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,GAAG,CAAC,EAAE,OAAO,CAAC;IACd,MAAM,CAAC,EAAE,MAAM,CAAC;CACnB;AAED,wBAAsB,KAAK,CAAC,IAAI,EAAE,YAAY,iBA0F7C"}
|
|
@@ -0,0 +1,221 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
// filename: src/tools/shell.ts
|
|
3
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
4
|
+
if (k2 === undefined) k2 = k;
|
|
5
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
6
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
7
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
8
|
+
}
|
|
9
|
+
Object.defineProperty(o, k2, desc);
|
|
10
|
+
}) : (function(o, m, k, k2) {
|
|
11
|
+
if (k2 === undefined) k2 = k;
|
|
12
|
+
o[k2] = m[k];
|
|
13
|
+
}));
|
|
14
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
15
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
16
|
+
}) : function(o, v) {
|
|
17
|
+
o["default"] = v;
|
|
18
|
+
});
|
|
19
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
20
|
+
if (mod && mod.__esModule) return mod;
|
|
21
|
+
var result = {};
|
|
22
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
23
|
+
__setModuleDefault(result, mod);
|
|
24
|
+
return result;
|
|
25
|
+
};
|
|
26
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
27
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
28
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
29
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
30
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
31
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
32
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
33
|
+
});
|
|
34
|
+
};
|
|
35
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
36
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
37
|
+
};
|
|
38
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
39
|
+
exports.shell = shell;
|
|
40
|
+
const fs = __importStar(require("node:fs"));
|
|
41
|
+
const Path = __importStar(require("node:path"));
|
|
42
|
+
const os = __importStar(require("node:os"));
|
|
43
|
+
const readline = __importStar(require("node:readline"));
|
|
44
|
+
const chalk_1 = __importDefault(require("chalk"));
|
|
45
|
+
const common_1 = require("./common");
|
|
46
|
+
const fs_1 = require("../utils/fs");
|
|
47
|
+
const tar = __importStar(require("tar"));
|
|
48
|
+
const yaml = __importStar(require("yaml"));
|
|
49
|
+
function shell(opts) {
|
|
50
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
51
|
+
require('dotenv').config();
|
|
52
|
+
const { psm, driver, home } = yield (0, common_1.psmLockup)({ schema: opts.schema, driver: opts.driver });
|
|
53
|
+
const migrator = driver.migrator({
|
|
54
|
+
url: process.env[psm.psm.url],
|
|
55
|
+
migrate: "",
|
|
56
|
+
check: "",
|
|
57
|
+
core: "",
|
|
58
|
+
});
|
|
59
|
+
console.log(chalk_1.default.cyan("🐚 PSM Shell Interativo"));
|
|
60
|
+
console.log(chalk_1.default.gray("Digite comandos SQL (terminados com ';'). Comandos especiais:"));
|
|
61
|
+
console.log(chalk_1.default.gray(" \\commit - commita a transação e gera revisão"));
|
|
62
|
+
console.log(chalk_1.default.gray(" \\rollback - desfaz a transação e sai"));
|
|
63
|
+
console.log(chalk_1.default.gray(" \\exit - sai sem commitar (rollback implícito)"));
|
|
64
|
+
// Inicia transação
|
|
65
|
+
const beginResult = yield migrator.execute([{ raw: "BEGIN", group: "shell", filename: "shell" }]);
|
|
66
|
+
if (!beginResult.success) {
|
|
67
|
+
console.error(chalk_1.default.red("Erro ao iniciar transação:"), beginResult.error);
|
|
68
|
+
return;
|
|
69
|
+
}
|
|
70
|
+
const successfulCommands = [];
|
|
71
|
+
const rl = readline.createInterface({
|
|
72
|
+
input: process.stdin,
|
|
73
|
+
output: process.stdout,
|
|
74
|
+
prompt: chalk_1.default.green("psm> "),
|
|
75
|
+
});
|
|
76
|
+
let currentCommand = "";
|
|
77
|
+
rl.prompt();
|
|
78
|
+
rl.on('line', (line) => __awaiter(this, void 0, void 0, function* () {
|
|
79
|
+
const trimmed = line.trim();
|
|
80
|
+
// Comandos especiais
|
|
81
|
+
if (trimmed === "\\commit") {
|
|
82
|
+
yield commitTransaction(migrator, successfulCommands, psm, home, opts);
|
|
83
|
+
rl.close();
|
|
84
|
+
return;
|
|
85
|
+
}
|
|
86
|
+
if (trimmed === "\\rollback") {
|
|
87
|
+
yield rollbackTransaction(migrator);
|
|
88
|
+
console.log(chalk_1.default.yellow("Transação revertida. Nenhuma revisão gerada."));
|
|
89
|
+
rl.close();
|
|
90
|
+
return;
|
|
91
|
+
}
|
|
92
|
+
if (trimmed === "\\exit") {
|
|
93
|
+
yield rollbackTransaction(migrator);
|
|
94
|
+
console.log(chalk_1.default.yellow("Saindo sem commitar."));
|
|
95
|
+
rl.close();
|
|
96
|
+
return;
|
|
97
|
+
}
|
|
98
|
+
// Acumula linhas até encontrar ';' no final (ignorando espaços)
|
|
99
|
+
currentCommand += line + "\n";
|
|
100
|
+
if (line.trim().endsWith(';')) {
|
|
101
|
+
const sql = currentCommand.trim();
|
|
102
|
+
if (sql) {
|
|
103
|
+
try {
|
|
104
|
+
const result = yield migrator.execute([{ raw: sql, group: "shell", filename: "shell" }]);
|
|
105
|
+
if (result.success) {
|
|
106
|
+
console.log(chalk_1.default.green("✔ Comando executado com sucesso."));
|
|
107
|
+
successfulCommands.push(sql);
|
|
108
|
+
}
|
|
109
|
+
else {
|
|
110
|
+
console.error(chalk_1.default.red("✘ Erro ao executar comando:"), result.error);
|
|
111
|
+
// Em caso de erro, podemos dar rollback imediato
|
|
112
|
+
yield rollbackTransaction(migrator);
|
|
113
|
+
console.log(chalk_1.default.red("Transação revertida devido a erro. Encerrando."));
|
|
114
|
+
rl.close();
|
|
115
|
+
return;
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
catch (err) {
|
|
119
|
+
console.error(chalk_1.default.red("Exceção:"), err.message);
|
|
120
|
+
yield rollbackTransaction(migrator);
|
|
121
|
+
rl.close();
|
|
122
|
+
return;
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
currentCommand = "";
|
|
126
|
+
rl.prompt();
|
|
127
|
+
}
|
|
128
|
+
else {
|
|
129
|
+
rl.prompt();
|
|
130
|
+
}
|
|
131
|
+
})).on('close', () => {
|
|
132
|
+
process.exit(0);
|
|
133
|
+
});
|
|
134
|
+
});
|
|
135
|
+
}
|
|
136
|
+
function commitTransaction(migrator, commands, psm, home, opts) {
|
|
137
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
138
|
+
const commitResult = yield migrator.execute([{ raw: "COMMIT", group: "shell", filename: "shell" }]);
|
|
139
|
+
if (!commitResult.success) {
|
|
140
|
+
console.error(chalk_1.default.red("Erro ao commitar transação:"), commitResult.error);
|
|
141
|
+
return;
|
|
142
|
+
}
|
|
143
|
+
console.log(chalk_1.default.green("✔ Transação commitada."));
|
|
144
|
+
// Gera revisão com os comandos bem-sucedidos
|
|
145
|
+
yield createRevisionFromCommands(commands, psm, home, opts);
|
|
146
|
+
});
|
|
147
|
+
}
|
|
148
|
+
function rollbackTransaction(migrator) {
|
|
149
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
150
|
+
const rollbackResult = yield migrator.execute([{ raw: "ROLLBACK", group: "shell", filename: "shell" }]);
|
|
151
|
+
if (!rollbackResult.success) {
|
|
152
|
+
console.error(chalk_1.default.red("Erro ao fazer rollback:"), rollbackResult.error);
|
|
153
|
+
}
|
|
154
|
+
});
|
|
155
|
+
}
|
|
156
|
+
function createRevisionFromCommands(commands, psm, home, opts) {
|
|
157
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
158
|
+
var _a;
|
|
159
|
+
if (commands.length === 0) {
|
|
160
|
+
console.log(chalk_1.default.yellow("Nenhum comando bem-sucedido para gerar revisão."));
|
|
161
|
+
return;
|
|
162
|
+
}
|
|
163
|
+
const moment = require('moment');
|
|
164
|
+
const now = moment();
|
|
165
|
+
const instante = now.format('YYYYMMDDHHmmss');
|
|
166
|
+
const safeLabel = opts.label ? ` - ${(0, fs_1.sanitizeLabel)(opts.label)}` : '';
|
|
167
|
+
const revDirName = `${instante}${safeLabel}`;
|
|
168
|
+
const revisionsBase = Path.join(home, "psm/revisions/schema");
|
|
169
|
+
const tempDir = fs.mkdtempSync(Path.join(os.tmpdir(), "psm-shell-"));
|
|
170
|
+
try {
|
|
171
|
+
// Cria migration.sql com todos os comandos
|
|
172
|
+
const migrationContent = commands.join(';\n') + (commands.length ? ';' : '');
|
|
173
|
+
fs.writeFileSync(Path.join(tempDir, "migration.sql"), migrationContent);
|
|
174
|
+
// Gera psm.yml básico
|
|
175
|
+
const revPsm = {
|
|
176
|
+
psm: {
|
|
177
|
+
migration: psm.psm.migration,
|
|
178
|
+
driver: psm.psm.driver,
|
|
179
|
+
url: psm.psm.url,
|
|
180
|
+
sys: psm.psm.sys,
|
|
181
|
+
output: psm.psm.output,
|
|
182
|
+
schema: psm.psm.schema,
|
|
183
|
+
},
|
|
184
|
+
test: {
|
|
185
|
+
check: "skipped",
|
|
186
|
+
success: true,
|
|
187
|
+
messages: [],
|
|
188
|
+
},
|
|
189
|
+
migration: {
|
|
190
|
+
revision: `${instante} - ${psm.psm.migration}`,
|
|
191
|
+
instante: instante,
|
|
192
|
+
preview: (_a = psm.migration) === null || _a === void 0 ? void 0 : _a.revision, // último preview se houver
|
|
193
|
+
label: opts.label || "",
|
|
194
|
+
},
|
|
195
|
+
};
|
|
196
|
+
fs.writeFileSync(Path.join(tempDir, "psm.yml"), yaml.stringify(revPsm));
|
|
197
|
+
// Cria o diretório de destino com o nome da revisão
|
|
198
|
+
const targetDir = Path.join(revisionsBase, revDirName);
|
|
199
|
+
fs.mkdirSync(targetDir, { recursive: true });
|
|
200
|
+
fs.cpSync(tempDir, targetDir, { recursive: true });
|
|
201
|
+
// Compacta
|
|
202
|
+
const archiveName = Path.join(revisionsBase, `${revDirName}.tar.gz`);
|
|
203
|
+
yield tar.c({
|
|
204
|
+
gzip: { level: 9 },
|
|
205
|
+
file: archiveName,
|
|
206
|
+
cwd: revisionsBase,
|
|
207
|
+
}, [revDirName]);
|
|
208
|
+
console.log(chalk_1.default.green(`✔ Revisão gerada: ${archiveName}`));
|
|
209
|
+
// Remove diretório descompactado
|
|
210
|
+
fs.rmSync(targetDir, { recursive: true, force: true });
|
|
211
|
+
// Adiciona ao git se solicitado
|
|
212
|
+
if (opts.add) {
|
|
213
|
+
(0, fs_1.gitAddPath)(home, archiveName);
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
finally {
|
|
217
|
+
fs.rmSync(tempDir, { recursive: true, force: true });
|
|
218
|
+
}
|
|
219
|
+
});
|
|
220
|
+
}
|
|
221
|
+
//# sourceMappingURL=shell.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"shell.js","sourceRoot":"","sources":["shell.ts"],"names":[],"mappings":";AAAA,+BAA+B;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAqB/B,sB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E;gBAClB,IAAI,EAAE,WAAW;gBACjB,GAAG,EAAE,aAAa;aACrB,EACD,CAAC,UAAU,CAAC,CACf,CAAC;YAEF,OAAO,CAAC,GAAG,CAAC,eAAK,CAAC,KAAK,CAAC,qBAAqB,WAAW,EAAE,CAAC,CAAC,CAAC;YAE7D,iCAAiC;YACjC,EAAE,CAAC,MAAM,CAAC,SAAS,EAAE,EAAE,SAAS,EAAE,IAAI,EAAE,KAAK,EAAE,IAAI,EAAE,CAAC,CAAC;YAEvD,gCAAgC;YAChC,IAAI,IAAI,CAAC,GAAG,EAAE,CAAC;gBACX,IAAA,eAAU,EAAC,IAAI,EAAE,WAAW,CAAC,CAAC;YAClC,CAAC;QACL,CAAC;gBAAS,CAAC;YACP,EAAE,CAAC,MAAM,CAAC,OAAO,EAAE,EAAE,SAAS,EAAE,IAAI,EAAE,KAAK,EAAE,IAAI,EAAE,CAAC,CAAC;QACzD,CAAC;IACL,CAAC;CAAA"}
|
|
@@ -0,0 +1,210 @@
|
|
|
1
|
+
// filename: src/tools/shell.ts
|
|
2
|
+
|
|
3
|
+
import * as fs from "node:fs";
|
|
4
|
+
import * as Path from "node:path";
|
|
5
|
+
import * as os from "node:os";
|
|
6
|
+
import * as readline from "node:readline";
|
|
7
|
+
import chalk from "chalk";
|
|
8
|
+
import { PSMMigrator } from "../driver";
|
|
9
|
+
import { psmLockup } from "./common";
|
|
10
|
+
import { gitAddPath, sanitizeLabel } from "../utils/fs";
|
|
11
|
+
import { PSMConfigFile } from "../configs";
|
|
12
|
+
import * as tar from "tar";
|
|
13
|
+
import * as yaml from "yaml";
|
|
14
|
+
|
|
15
|
+
export interface ShellOptions {
|
|
16
|
+
schema?: string;
|
|
17
|
+
label?: string;
|
|
18
|
+
add?: boolean; // adicionar ao git após gerar revisão
|
|
19
|
+
driver?: string;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export async function shell(opts: ShellOptions) {
|
|
23
|
+
require('dotenv').config();
|
|
24
|
+
|
|
25
|
+
const { psm, driver, home } = await psmLockup({ schema: opts.schema, driver: opts.driver });
|
|
26
|
+
|
|
27
|
+
const migrator = driver.migrator({
|
|
28
|
+
url: process.env[psm.psm.url],
|
|
29
|
+
migrate: "",
|
|
30
|
+
check: "",
|
|
31
|
+
core: "",
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
console.log(chalk.cyan("🐚 PSM Shell Interativo"));
|
|
35
|
+
console.log(chalk.gray("Digite comandos SQL (terminados com ';'). Comandos especiais:"));
|
|
36
|
+
console.log(chalk.gray(" \\commit - commita a transação e gera revisão"));
|
|
37
|
+
console.log(chalk.gray(" \\rollback - desfaz a transação e sai"));
|
|
38
|
+
console.log(chalk.gray(" \\exit - sai sem commitar (rollback implícito)"));
|
|
39
|
+
|
|
40
|
+
// Inicia transação
|
|
41
|
+
const beginResult = await migrator.execute([{ raw: "BEGIN", group: "shell", filename: "shell" }]);
|
|
42
|
+
if (!beginResult.success) {
|
|
43
|
+
console.error(chalk.red("Erro ao iniciar transação:"), beginResult.error);
|
|
44
|
+
return;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
const successfulCommands: string[] = [];
|
|
48
|
+
const rl = readline.createInterface({
|
|
49
|
+
input: process.stdin,
|
|
50
|
+
output: process.stdout,
|
|
51
|
+
prompt: chalk.green("psm> "),
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
let currentCommand = "";
|
|
55
|
+
rl.prompt();
|
|
56
|
+
|
|
57
|
+
rl.on('line', async (line) => {
|
|
58
|
+
const trimmed = line.trim();
|
|
59
|
+
|
|
60
|
+
// Comandos especiais
|
|
61
|
+
if (trimmed === "\\commit") {
|
|
62
|
+
await commitTransaction(migrator, successfulCommands, psm, home, opts);
|
|
63
|
+
rl.close();
|
|
64
|
+
return;
|
|
65
|
+
}
|
|
66
|
+
if (trimmed === "\\rollback") {
|
|
67
|
+
await rollbackTransaction(migrator);
|
|
68
|
+
console.log(chalk.yellow("Transação revertida. Nenhuma revisão gerada."));
|
|
69
|
+
rl.close();
|
|
70
|
+
return;
|
|
71
|
+
}
|
|
72
|
+
if (trimmed === "\\exit") {
|
|
73
|
+
await rollbackTransaction(migrator);
|
|
74
|
+
console.log(chalk.yellow("Saindo sem commitar."));
|
|
75
|
+
rl.close();
|
|
76
|
+
return;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
// Acumula linhas até encontrar ';' no final (ignorando espaços)
|
|
80
|
+
currentCommand += line + "\n";
|
|
81
|
+
if (line.trim().endsWith(';')) {
|
|
82
|
+
const sql = currentCommand.trim();
|
|
83
|
+
if (sql) {
|
|
84
|
+
try {
|
|
85
|
+
const result = await migrator.execute([{ raw: sql, group: "shell", filename: "shell" }]);
|
|
86
|
+
if (result.success) {
|
|
87
|
+
console.log(chalk.green("✔ Comando executado com sucesso."));
|
|
88
|
+
successfulCommands.push(sql);
|
|
89
|
+
} else {
|
|
90
|
+
console.error(chalk.red("✘ Erro ao executar comando:"), result.error);
|
|
91
|
+
// Em caso de erro, podemos dar rollback imediato
|
|
92
|
+
await rollbackTransaction(migrator);
|
|
93
|
+
console.log(chalk.red("Transação revertida devido a erro. Encerrando."));
|
|
94
|
+
rl.close();
|
|
95
|
+
return;
|
|
96
|
+
}
|
|
97
|
+
} catch (err: any) {
|
|
98
|
+
console.error(chalk.red("Exceção:"), err.message);
|
|
99
|
+
await rollbackTransaction(migrator);
|
|
100
|
+
rl.close();
|
|
101
|
+
return;
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
currentCommand = "";
|
|
105
|
+
rl.prompt();
|
|
106
|
+
} else {
|
|
107
|
+
rl.prompt();
|
|
108
|
+
}
|
|
109
|
+
}).on('close', () => {
|
|
110
|
+
process.exit(0);
|
|
111
|
+
});
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
async function commitTransaction(
|
|
115
|
+
migrator: PSMMigrator,
|
|
116
|
+
commands: string[],
|
|
117
|
+
psm: any,
|
|
118
|
+
home: string,
|
|
119
|
+
opts: ShellOptions
|
|
120
|
+
) {
|
|
121
|
+
const commitResult = await migrator.execute([{ raw: "COMMIT", group: "shell", filename: "shell" }]);
|
|
122
|
+
if (!commitResult.success) {
|
|
123
|
+
console.error(chalk.red("Erro ao commitar transação:"), commitResult.error);
|
|
124
|
+
return;
|
|
125
|
+
}
|
|
126
|
+
console.log(chalk.green("✔ Transação commitada."));
|
|
127
|
+
|
|
128
|
+
// Gera revisão com os comandos bem-sucedidos
|
|
129
|
+
await createRevisionFromCommands(commands, psm, home, opts);
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
async function rollbackTransaction(migrator: PSMMigrator) {
|
|
133
|
+
const rollbackResult = await migrator.execute([{ raw: "ROLLBACK", group: "shell", filename: "shell" }]);
|
|
134
|
+
if (!rollbackResult.success) {
|
|
135
|
+
console.error(chalk.red("Erro ao fazer rollback:"), rollbackResult.error);
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
async function createRevisionFromCommands(commands: string[], psm: any, home: string, opts: ShellOptions) {
|
|
140
|
+
if (commands.length === 0) {
|
|
141
|
+
console.log(chalk.yellow("Nenhum comando bem-sucedido para gerar revisão."));
|
|
142
|
+
return;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
const moment = require('moment');
|
|
146
|
+
const now = moment();
|
|
147
|
+
const instante = now.format('YYYYMMDDHHmmss');
|
|
148
|
+
const safeLabel = opts.label ? ` - ${sanitizeLabel(opts.label)}` : '';
|
|
149
|
+
const revDirName = `${instante}${safeLabel}`;
|
|
150
|
+
const revisionsBase = Path.join(home, "psm/revisions/schema");
|
|
151
|
+
const tempDir = fs.mkdtempSync(Path.join(os.tmpdir(), "psm-shell-"));
|
|
152
|
+
|
|
153
|
+
try {
|
|
154
|
+
// Cria migration.sql com todos os comandos
|
|
155
|
+
const migrationContent = commands.join(';\n') + (commands.length ? ';' : '');
|
|
156
|
+
fs.writeFileSync(Path.join(tempDir, "migration.sql"), migrationContent);
|
|
157
|
+
|
|
158
|
+
// Gera psm.yml básico
|
|
159
|
+
const revPsm: PSMConfigFile = {
|
|
160
|
+
psm: {
|
|
161
|
+
migration: psm.psm.migration,
|
|
162
|
+
driver: psm.psm.driver,
|
|
163
|
+
url: psm.psm.url,
|
|
164
|
+
sys: psm.psm.sys,
|
|
165
|
+
output: psm.psm.output,
|
|
166
|
+
schema: psm.psm.schema,
|
|
167
|
+
},
|
|
168
|
+
test: {
|
|
169
|
+
check: "skipped",
|
|
170
|
+
success: true,
|
|
171
|
+
messages: [],
|
|
172
|
+
},
|
|
173
|
+
migration: {
|
|
174
|
+
revision: `${instante} - ${psm.psm.migration}`,
|
|
175
|
+
instante: instante,
|
|
176
|
+
preview: psm.migration?.revision, // último preview se houver
|
|
177
|
+
label: opts.label || "",
|
|
178
|
+
},
|
|
179
|
+
};
|
|
180
|
+
fs.writeFileSync(Path.join(tempDir, "psm.yml"), yaml.stringify(revPsm));
|
|
181
|
+
|
|
182
|
+
// Cria o diretório de destino com o nome da revisão
|
|
183
|
+
const targetDir = Path.join(revisionsBase, revDirName);
|
|
184
|
+
fs.mkdirSync(targetDir, { recursive: true });
|
|
185
|
+
fs.cpSync(tempDir, targetDir, { recursive: true });
|
|
186
|
+
|
|
187
|
+
// Compacta
|
|
188
|
+
const archiveName = Path.join(revisionsBase, `${revDirName}.tar.gz`);
|
|
189
|
+
await tar.c(
|
|
190
|
+
{
|
|
191
|
+
gzip: { level: 9 },
|
|
192
|
+
file: archiveName,
|
|
193
|
+
cwd: revisionsBase,
|
|
194
|
+
},
|
|
195
|
+
[revDirName]
|
|
196
|
+
);
|
|
197
|
+
|
|
198
|
+
console.log(chalk.green(`✔ Revisão gerada: ${archiveName}`));
|
|
199
|
+
|
|
200
|
+
// Remove diretório descompactado
|
|
201
|
+
fs.rmSync(targetDir, { recursive: true, force: true });
|
|
202
|
+
|
|
203
|
+
// Adiciona ao git se solicitado
|
|
204
|
+
if (opts.add) {
|
|
205
|
+
gitAddPath(home, archiveName);
|
|
206
|
+
}
|
|
207
|
+
} finally {
|
|
208
|
+
fs.rmSync(tempDir, { recursive: true, force: true });
|
|
209
|
+
}
|
|
210
|
+
}
|