create-modoock-app 2.2.1 → 2.3.0
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/dist/cli.js +20 -107
- package/dist/commands/create.js +52 -0
- package/dist/commands/init.js +24 -0
- package/dist/commands/update.js +171 -0
- package/dist/constants.js +18 -0
- package/dist/utils/backup.js +22 -0
- package/dist/utils/files.js +33 -0
- package/dist/utils/metadata.js +48 -0
- package/dist/utils/packages.js +49 -0
- package/package.json +1 -1
package/dist/cli.js
CHANGED
|
@@ -1,110 +1,28 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
"use strict";
|
|
3
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
4
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
5
|
-
};
|
|
6
3
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
7
|
-
const
|
|
8
|
-
const
|
|
9
|
-
const
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
withFileTypes: true,
|
|
18
|
-
})) {
|
|
19
|
-
const sourcePath = node_path_1.default.join(source, entry.name);
|
|
20
|
-
const destinationPath = node_path_1.default.join(destination, entry.name);
|
|
21
|
-
if (entry.isDirectory()) {
|
|
22
|
-
copyDirectory(sourcePath, destinationPath);
|
|
23
|
-
}
|
|
24
|
-
else {
|
|
25
|
-
node_fs_1.default.copyFileSync(sourcePath, destinationPath);
|
|
26
|
-
}
|
|
27
|
-
}
|
|
28
|
-
}
|
|
29
|
-
function getLatestVersion(packageName) {
|
|
30
|
-
try {
|
|
31
|
-
const version = (0, node_child_process_1.execSync)(`npm view ${packageName} version`, {
|
|
32
|
-
encoding: "utf8",
|
|
33
|
-
stdio: ["ignore", "pipe", "pipe"],
|
|
34
|
-
}).trim();
|
|
35
|
-
if (!version) {
|
|
36
|
-
throw new Error(`No stable version found for ${packageName}.`);
|
|
37
|
-
}
|
|
38
|
-
return version;
|
|
39
|
-
}
|
|
40
|
-
catch {
|
|
41
|
-
throw new Error(`Could not resolve the latest stable version of ${packageName}.`);
|
|
42
|
-
}
|
|
43
|
-
}
|
|
44
|
-
function getLatestModoockDependencies() {
|
|
45
|
-
const dependencies = {};
|
|
46
|
-
for (const packageName of MODOOCK_PACKAGES) {
|
|
47
|
-
console.log(`Resolving latest ${packageName}...`);
|
|
48
|
-
const version = getLatestVersion(packageName);
|
|
49
|
-
dependencies[packageName] = version;
|
|
50
|
-
console.log(` ${packageName}@${version}`);
|
|
51
|
-
}
|
|
52
|
-
return dependencies;
|
|
53
|
-
}
|
|
54
|
-
function updateProjectPackageJson(targetDirectory) {
|
|
55
|
-
const packageJsonPath = node_path_1.default.join(targetDirectory, "package.json");
|
|
56
|
-
const packageJson = JSON.parse(node_fs_1.default.readFileSync(packageJsonPath, "utf8"));
|
|
57
|
-
packageJson.name =
|
|
58
|
-
node_path_1.default.basename(targetDirectory);
|
|
59
|
-
const modoockDependencies = getLatestModoockDependencies();
|
|
60
|
-
packageJson.dependencies = {
|
|
61
|
-
...(packageJson.dependencies ?? {}),
|
|
62
|
-
...modoockDependencies,
|
|
63
|
-
};
|
|
64
|
-
node_fs_1.default.writeFileSync(packageJsonPath, JSON.stringify(packageJson, null, 2) + "\n");
|
|
65
|
-
}
|
|
66
|
-
function installDependencies(targetDirectory) {
|
|
67
|
-
console.log("");
|
|
68
|
-
console.log("Installing dependencies...");
|
|
69
|
-
try {
|
|
70
|
-
(0, node_child_process_1.execSync)("npm install", {
|
|
71
|
-
cwd: targetDirectory,
|
|
72
|
-
stdio: "inherit",
|
|
73
|
-
});
|
|
74
|
-
}
|
|
75
|
-
catch {
|
|
76
|
-
throw new Error("Failed to install project dependencies.");
|
|
77
|
-
}
|
|
78
|
-
}
|
|
79
|
-
function main() {
|
|
80
|
-
const projectName = process.argv[2];
|
|
81
|
-
if (!projectName) {
|
|
82
|
-
console.error("Usage: npx create-modoock-app <project-name>");
|
|
83
|
-
process.exit(1);
|
|
84
|
-
}
|
|
85
|
-
const targetDirectory = node_path_1.default.resolve(process.cwd(), projectName);
|
|
86
|
-
if (node_fs_1.default.existsSync(targetDirectory)) {
|
|
87
|
-
console.error(`Error: Directory "${projectName}" already exists.`);
|
|
4
|
+
const create_1 = require("./commands/create");
|
|
5
|
+
const init_1 = require("./commands/init");
|
|
6
|
+
const update_1 = require("./commands/update");
|
|
7
|
+
async function main() {
|
|
8
|
+
const command = process.argv[2];
|
|
9
|
+
if (!command) {
|
|
10
|
+
console.error("Usage:");
|
|
11
|
+
console.error(" npx create-modoock-app <project-name>");
|
|
12
|
+
console.error(" npx create-modoock-app init");
|
|
13
|
+
console.error(" npx create-modoock-app update");
|
|
88
14
|
process.exit(1);
|
|
89
15
|
}
|
|
90
|
-
const packageRoot = node_path_1.default.resolve(__dirname, "..");
|
|
91
|
-
const templateDirectory = node_path_1.default.join(packageRoot, "template");
|
|
92
|
-
console.log(`Creating Modooock project: ${node_path_1.default.basename(targetDirectory)}`);
|
|
93
16
|
try {
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
console.log(`Project created at: ${targetDirectory}`);
|
|
104
|
-
console.log("");
|
|
105
|
-
console.log("Next steps:");
|
|
106
|
-
console.log(` cd ${projectName}`);
|
|
107
|
-
console.log(" npm run dev");
|
|
17
|
+
if (command === "init") {
|
|
18
|
+
(0, init_1.initializeProject)();
|
|
19
|
+
return;
|
|
20
|
+
}
|
|
21
|
+
if (command === "update") {
|
|
22
|
+
await (0, update_1.updateProject)();
|
|
23
|
+
return;
|
|
24
|
+
}
|
|
25
|
+
(0, create_1.createProject)(command);
|
|
108
26
|
}
|
|
109
27
|
catch (error) {
|
|
110
28
|
console.error("");
|
|
@@ -112,13 +30,8 @@ function main() {
|
|
|
112
30
|
console.error(error.message);
|
|
113
31
|
}
|
|
114
32
|
else {
|
|
115
|
-
console.error("
|
|
33
|
+
console.error("Something went wrong.");
|
|
116
34
|
}
|
|
117
|
-
// Do not leave a partially generated project.
|
|
118
|
-
node_fs_1.default.rmSync(targetDirectory, {
|
|
119
|
-
recursive: true,
|
|
120
|
-
force: true,
|
|
121
|
-
});
|
|
122
35
|
process.exit(1);
|
|
123
36
|
}
|
|
124
37
|
}
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.createProject = createProject;
|
|
7
|
+
const node_fs_1 = __importDefault(require("node:fs"));
|
|
8
|
+
const node_path_1 = __importDefault(require("node:path"));
|
|
9
|
+
const files_1 = require("../utils/files");
|
|
10
|
+
const metadata_1 = require("../utils/metadata");
|
|
11
|
+
const packages_1 = require("../utils/packages");
|
|
12
|
+
function updateProjectPackageJson(targetDirectory) {
|
|
13
|
+
const packageJsonPath = node_path_1.default.join(targetDirectory, "package.json");
|
|
14
|
+
const packageJson = JSON.parse(node_fs_1.default.readFileSync(packageJsonPath, "utf8"));
|
|
15
|
+
packageJson.name =
|
|
16
|
+
node_path_1.default.basename(targetDirectory);
|
|
17
|
+
const modoockDependencies = (0, packages_1.getLatestModoockDependencies)();
|
|
18
|
+
packageJson.dependencies = {
|
|
19
|
+
...(packageJson.dependencies ?? {}),
|
|
20
|
+
...modoockDependencies,
|
|
21
|
+
};
|
|
22
|
+
node_fs_1.default.writeFileSync(packageJsonPath, JSON.stringify(packageJson, null, 2) + "\n");
|
|
23
|
+
}
|
|
24
|
+
function createProject(projectName) {
|
|
25
|
+
const targetDirectory = node_path_1.default.resolve(process.cwd(), projectName);
|
|
26
|
+
if (node_fs_1.default.existsSync(targetDirectory)) {
|
|
27
|
+
throw new Error(`Directory "${projectName}" already exists.`);
|
|
28
|
+
}
|
|
29
|
+
const templateDirectory = node_path_1.default.resolve(__dirname, "..", "..", "template");
|
|
30
|
+
console.log(`Creating Modoock project: ${node_path_1.default.basename(targetDirectory)}`);
|
|
31
|
+
try {
|
|
32
|
+
(0, files_1.copyDirectory)(templateDirectory, targetDirectory);
|
|
33
|
+
updateProjectPackageJson(targetDirectory);
|
|
34
|
+
(0, metadata_1.writeModoockMetadata)(targetDirectory);
|
|
35
|
+
(0, packages_1.installDependencies)(targetDirectory);
|
|
36
|
+
console.log("");
|
|
37
|
+
console.log("Done!");
|
|
38
|
+
console.log("");
|
|
39
|
+
console.log(`Project created at: ${targetDirectory}`);
|
|
40
|
+
console.log("");
|
|
41
|
+
console.log("Next steps:");
|
|
42
|
+
console.log(` cd ${projectName}`);
|
|
43
|
+
console.log(" npm run dev");
|
|
44
|
+
}
|
|
45
|
+
catch (error) {
|
|
46
|
+
node_fs_1.default.rmSync(targetDirectory, {
|
|
47
|
+
recursive: true,
|
|
48
|
+
force: true,
|
|
49
|
+
});
|
|
50
|
+
throw error;
|
|
51
|
+
}
|
|
52
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.initializeProject = initializeProject;
|
|
7
|
+
const node_fs_1 = __importDefault(require("node:fs"));
|
|
8
|
+
const node_path_1 = __importDefault(require("node:path"));
|
|
9
|
+
const constants_1 = require("../constants");
|
|
10
|
+
const metadata_1 = require("../utils/metadata");
|
|
11
|
+
function initializeProject() {
|
|
12
|
+
const targetDirectory = process.cwd();
|
|
13
|
+
if (!node_fs_1.default.existsSync(node_path_1.default.join(targetDirectory, "package.json"))) {
|
|
14
|
+
throw new Error("No package.json found in the current directory.");
|
|
15
|
+
}
|
|
16
|
+
const metadataPath = node_path_1.default.join(targetDirectory, constants_1.MODOOCK_DIRECTORY, constants_1.METADATA_FILE);
|
|
17
|
+
if (node_fs_1.default.existsSync(metadataPath)) {
|
|
18
|
+
console.log("This project is already initialized for Modoock updates.");
|
|
19
|
+
return;
|
|
20
|
+
}
|
|
21
|
+
(0, metadata_1.writeModoockMetadata)(targetDirectory);
|
|
22
|
+
console.log("Modoock project initialized successfully.");
|
|
23
|
+
console.log("No project files were modified.");
|
|
24
|
+
}
|
|
@@ -0,0 +1,171 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.updateProject = updateProject;
|
|
7
|
+
const node_fs_1 = __importDefault(require("node:fs"));
|
|
8
|
+
const node_path_1 = __importDefault(require("node:path"));
|
|
9
|
+
const node_readline_1 = __importDefault(require("node:readline"));
|
|
10
|
+
const constants_1 = require("../constants");
|
|
11
|
+
const backup_1 = require("../utils/backup");
|
|
12
|
+
const files_1 = require("../utils/files");
|
|
13
|
+
const metadata_1 = require("../utils/metadata");
|
|
14
|
+
const packages_1 = require("../utils/packages");
|
|
15
|
+
function askQuestion(question) {
|
|
16
|
+
const rl = node_readline_1.default.createInterface({
|
|
17
|
+
input: process.stdin,
|
|
18
|
+
output: process.stdout,
|
|
19
|
+
});
|
|
20
|
+
return new Promise((resolve) => {
|
|
21
|
+
rl.question(question, (answer) => {
|
|
22
|
+
rl.close();
|
|
23
|
+
resolve(answer.trim());
|
|
24
|
+
});
|
|
25
|
+
});
|
|
26
|
+
}
|
|
27
|
+
function getTemplateDirectory() {
|
|
28
|
+
return node_path_1.default.resolve(__dirname, "..", "..", "template");
|
|
29
|
+
}
|
|
30
|
+
function shouldIgnorePath(relativePath) {
|
|
31
|
+
const normalized = relativePath.replace(/\\/g, "/");
|
|
32
|
+
if (normalized ===
|
|
33
|
+
constants_1.MODOOCK_DIRECTORY) {
|
|
34
|
+
return true;
|
|
35
|
+
}
|
|
36
|
+
if (normalized.startsWith(`${constants_1.MODOOCK_DIRECTORY}/`)) {
|
|
37
|
+
return true;
|
|
38
|
+
}
|
|
39
|
+
if (normalized ===
|
|
40
|
+
"package.json") {
|
|
41
|
+
return true;
|
|
42
|
+
}
|
|
43
|
+
if (normalized ===
|
|
44
|
+
"package-lock.json") {
|
|
45
|
+
return true;
|
|
46
|
+
}
|
|
47
|
+
return false;
|
|
48
|
+
}
|
|
49
|
+
function syncNewFilesAndFolders(targetDirectory) {
|
|
50
|
+
const templateDirectory = getTemplateDirectory();
|
|
51
|
+
function syncDirectory(templateDirectoryPath, projectDirectoryPath, relativeDirectory) {
|
|
52
|
+
if (!node_fs_1.default.existsSync(templateDirectoryPath)) {
|
|
53
|
+
return;
|
|
54
|
+
}
|
|
55
|
+
for (const entry of node_fs_1.default.readdirSync(templateDirectoryPath, {
|
|
56
|
+
withFileTypes: true,
|
|
57
|
+
})) {
|
|
58
|
+
const relativePath = relativeDirectory
|
|
59
|
+
? node_path_1.default.join(relativeDirectory, entry.name)
|
|
60
|
+
: entry.name;
|
|
61
|
+
if (shouldIgnorePath(relativePath)) {
|
|
62
|
+
continue;
|
|
63
|
+
}
|
|
64
|
+
const templatePath = node_path_1.default.join(templateDirectoryPath, entry.name);
|
|
65
|
+
const projectPath = node_path_1.default.join(projectDirectoryPath, entry.name);
|
|
66
|
+
if (entry.isDirectory()) {
|
|
67
|
+
if (!node_fs_1.default.existsSync(projectPath)) {
|
|
68
|
+
node_fs_1.default.cpSync(templatePath, projectPath, {
|
|
69
|
+
recursive: true,
|
|
70
|
+
});
|
|
71
|
+
console.log(`Added folder: ${relativePath}`);
|
|
72
|
+
continue;
|
|
73
|
+
}
|
|
74
|
+
syncDirectory(templatePath, projectPath, relativePath);
|
|
75
|
+
continue;
|
|
76
|
+
}
|
|
77
|
+
if (!node_fs_1.default.existsSync(projectPath)) {
|
|
78
|
+
node_fs_1.default.mkdirSync(node_path_1.default.dirname(projectPath), {
|
|
79
|
+
recursive: true,
|
|
80
|
+
});
|
|
81
|
+
node_fs_1.default.copyFileSync(templatePath, projectPath);
|
|
82
|
+
console.log(`Added file: ${relativePath}`);
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
syncDirectory(templateDirectory, targetDirectory, "");
|
|
87
|
+
}
|
|
88
|
+
async function updateManagedFiles(targetDirectory, metadata) {
|
|
89
|
+
const templateDirectory = getTemplateDirectory();
|
|
90
|
+
for (const relativePath of constants_1.MANAGED_FILES) {
|
|
91
|
+
const templatePath = node_path_1.default.join(templateDirectory, relativePath);
|
|
92
|
+
const projectPath = node_path_1.default.join(targetDirectory, relativePath);
|
|
93
|
+
if (!node_fs_1.default.existsSync(templatePath)) {
|
|
94
|
+
continue;
|
|
95
|
+
}
|
|
96
|
+
if (!node_fs_1.default.existsSync(projectPath)) {
|
|
97
|
+
node_fs_1.default.mkdirSync(node_path_1.default.dirname(projectPath), {
|
|
98
|
+
recursive: true,
|
|
99
|
+
});
|
|
100
|
+
node_fs_1.default.copyFileSync(templatePath, projectPath);
|
|
101
|
+
console.log(`Added: ${relativePath}`);
|
|
102
|
+
continue;
|
|
103
|
+
}
|
|
104
|
+
const originalHash = metadata.managedFiles?.[relativePath];
|
|
105
|
+
const currentHash = (0, files_1.getFileHash)(projectPath);
|
|
106
|
+
if (originalHash &&
|
|
107
|
+
currentHash ===
|
|
108
|
+
originalHash) {
|
|
109
|
+
node_fs_1.default.copyFileSync(templatePath, projectPath);
|
|
110
|
+
console.log(`Updated: ${relativePath}`);
|
|
111
|
+
continue;
|
|
112
|
+
}
|
|
113
|
+
console.log("");
|
|
114
|
+
console.log(`⚠️ Local changes detected: ${relativePath}`);
|
|
115
|
+
console.log("[1] Keep my version");
|
|
116
|
+
console.log("[2] Replace with Modoock version");
|
|
117
|
+
console.log("");
|
|
118
|
+
const answer = await askQuestion("Choose an option (1/2): ");
|
|
119
|
+
if (answer === "1") {
|
|
120
|
+
console.log(`Kept local version: ${relativePath}`);
|
|
121
|
+
continue;
|
|
122
|
+
}
|
|
123
|
+
if (answer === "2") {
|
|
124
|
+
console.log("");
|
|
125
|
+
console.log("⚠️ Your local file will be replaced.");
|
|
126
|
+
console.log("A backup will be created before replacement.");
|
|
127
|
+
const confirmation = await askQuestion("Continue? (y/N): ");
|
|
128
|
+
if (confirmation.toLowerCase() !==
|
|
129
|
+
"y") {
|
|
130
|
+
console.log(`Skipped: ${relativePath}`);
|
|
131
|
+
continue;
|
|
132
|
+
}
|
|
133
|
+
const backupPath = (0, backup_1.createBackup)(targetDirectory, relativePath);
|
|
134
|
+
node_fs_1.default.copyFileSync(templatePath, projectPath);
|
|
135
|
+
console.log(`Backup created: ${node_path_1.default.relative(targetDirectory, backupPath)}`);
|
|
136
|
+
console.log(`Updated: ${relativePath}`);
|
|
137
|
+
continue;
|
|
138
|
+
}
|
|
139
|
+
console.log(`Skipped: ${relativePath}`);
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
function updateProjectPackageJson(targetDirectory) {
|
|
143
|
+
const packageJsonPath = node_path_1.default.join(targetDirectory, "package.json");
|
|
144
|
+
const packageJson = JSON.parse(node_fs_1.default.readFileSync(packageJsonPath, "utf8"));
|
|
145
|
+
const modoockDependencies = (0, packages_1.getLatestModoockDependencies)();
|
|
146
|
+
packageJson.dependencies = {
|
|
147
|
+
...(packageJson.dependencies ?? {}),
|
|
148
|
+
...modoockDependencies,
|
|
149
|
+
};
|
|
150
|
+
node_fs_1.default.writeFileSync(packageJsonPath, JSON.stringify(packageJson, null, 2) + "\n");
|
|
151
|
+
}
|
|
152
|
+
async function updateProject() {
|
|
153
|
+
const targetDirectory = process.cwd();
|
|
154
|
+
console.log("Updating Modoock project...");
|
|
155
|
+
console.log("");
|
|
156
|
+
const metadata = (0, metadata_1.readModoockMetadata)(targetDirectory);
|
|
157
|
+
console.log(`Current Modoock generator version: ${metadata.generatorVersion}`);
|
|
158
|
+
console.log("");
|
|
159
|
+
// Add files and folders that are new
|
|
160
|
+
// in the Modoock template.
|
|
161
|
+
syncNewFilesAndFolders(targetDirectory);
|
|
162
|
+
// Update explicitly managed files.
|
|
163
|
+
await updateManagedFiles(targetDirectory, metadata);
|
|
164
|
+
console.log("");
|
|
165
|
+
console.log("Updating Modoock dependencies...");
|
|
166
|
+
updateProjectPackageJson(targetDirectory);
|
|
167
|
+
(0, packages_1.installDependencies)(targetDirectory);
|
|
168
|
+
(0, metadata_1.writeModoockMetadata)(targetDirectory);
|
|
169
|
+
console.log("");
|
|
170
|
+
console.log("Modoock update complete.");
|
|
171
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.METADATA_FILE = exports.MODOOCK_DIRECTORY = exports.MANAGED_FILES = exports.MODOOCK_PACKAGES = void 0;
|
|
4
|
+
exports.MODOOCK_PACKAGES = [
|
|
5
|
+
"@modoock/components",
|
|
6
|
+
"@modoock/knowledge-base",
|
|
7
|
+
];
|
|
8
|
+
exports.MANAGED_FILES = [
|
|
9
|
+
"app/globals.css",
|
|
10
|
+
"app/layout.tsx",
|
|
11
|
+
"components/layout/header.tsx",
|
|
12
|
+
"components/layout/app-layout.tsx",
|
|
13
|
+
"components/layout/side-nav.tsx",
|
|
14
|
+
"postcss.config.mjs",
|
|
15
|
+
"next.config.ts",
|
|
16
|
+
];
|
|
17
|
+
exports.MODOOCK_DIRECTORY = ".modoock";
|
|
18
|
+
exports.METADATA_FILE = "project.json";
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.createBackup = createBackup;
|
|
7
|
+
const node_fs_1 = __importDefault(require("node:fs"));
|
|
8
|
+
const node_path_1 = __importDefault(require("node:path"));
|
|
9
|
+
const constants_1 = require("../constants");
|
|
10
|
+
function createBackup(targetDirectory, relativePath) {
|
|
11
|
+
const timestamp = new Date()
|
|
12
|
+
.toISOString()
|
|
13
|
+
.replace(/[:.]/g, "-");
|
|
14
|
+
const backupDirectory = node_path_1.default.join(targetDirectory, constants_1.MODOOCK_DIRECTORY, "backups", timestamp);
|
|
15
|
+
const sourcePath = node_path_1.default.join(targetDirectory, relativePath);
|
|
16
|
+
const backupPath = node_path_1.default.join(backupDirectory, relativePath);
|
|
17
|
+
node_fs_1.default.mkdirSync(node_path_1.default.dirname(backupPath), {
|
|
18
|
+
recursive: true,
|
|
19
|
+
});
|
|
20
|
+
node_fs_1.default.copyFileSync(sourcePath, backupPath);
|
|
21
|
+
return backupPath;
|
|
22
|
+
}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.copyDirectory = copyDirectory;
|
|
7
|
+
exports.getFileHash = getFileHash;
|
|
8
|
+
const node_crypto_1 = __importDefault(require("node:crypto"));
|
|
9
|
+
const node_fs_1 = __importDefault(require("node:fs"));
|
|
10
|
+
const node_path_1 = __importDefault(require("node:path"));
|
|
11
|
+
function copyDirectory(source, destination) {
|
|
12
|
+
node_fs_1.default.mkdirSync(destination, {
|
|
13
|
+
recursive: true,
|
|
14
|
+
});
|
|
15
|
+
for (const entry of node_fs_1.default.readdirSync(source, {
|
|
16
|
+
withFileTypes: true,
|
|
17
|
+
})) {
|
|
18
|
+
const sourcePath = node_path_1.default.join(source, entry.name);
|
|
19
|
+
const destinationPath = node_path_1.default.join(destination, entry.name);
|
|
20
|
+
if (entry.isDirectory()) {
|
|
21
|
+
copyDirectory(sourcePath, destinationPath);
|
|
22
|
+
}
|
|
23
|
+
else {
|
|
24
|
+
node_fs_1.default.copyFileSync(sourcePath, destinationPath);
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
function getFileHash(filePath) {
|
|
29
|
+
return node_crypto_1.default
|
|
30
|
+
.createHash("sha256")
|
|
31
|
+
.update(node_fs_1.default.readFileSync(filePath))
|
|
32
|
+
.digest("hex");
|
|
33
|
+
}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.getPackageVersion = getPackageVersion;
|
|
7
|
+
exports.getManagedFileHashes = getManagedFileHashes;
|
|
8
|
+
exports.writeModoockMetadata = writeModoockMetadata;
|
|
9
|
+
exports.readModoockMetadata = readModoockMetadata;
|
|
10
|
+
const node_fs_1 = __importDefault(require("node:fs"));
|
|
11
|
+
const node_path_1 = __importDefault(require("node:path"));
|
|
12
|
+
const constants_1 = require("../constants");
|
|
13
|
+
const files_1 = require("./files");
|
|
14
|
+
function getPackageVersion() {
|
|
15
|
+
const packageJsonPath = node_path_1.default.resolve(__dirname, "..", "..", "package.json");
|
|
16
|
+
const packageJson = JSON.parse(node_fs_1.default.readFileSync(packageJsonPath, "utf8"));
|
|
17
|
+
return packageJson.version;
|
|
18
|
+
}
|
|
19
|
+
function getManagedFileHashes(targetDirectory) {
|
|
20
|
+
const hashes = {};
|
|
21
|
+
for (const relativePath of constants_1.MANAGED_FILES) {
|
|
22
|
+
const filePath = node_path_1.default.join(targetDirectory, relativePath);
|
|
23
|
+
if (node_fs_1.default.existsSync(filePath)) {
|
|
24
|
+
hashes[relativePath] =
|
|
25
|
+
(0, files_1.getFileHash)(filePath);
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
return hashes;
|
|
29
|
+
}
|
|
30
|
+
function writeModoockMetadata(targetDirectory) {
|
|
31
|
+
const modoockDirectory = node_path_1.default.join(targetDirectory, constants_1.MODOOCK_DIRECTORY);
|
|
32
|
+
node_fs_1.default.mkdirSync(modoockDirectory, {
|
|
33
|
+
recursive: true,
|
|
34
|
+
});
|
|
35
|
+
const metadata = {
|
|
36
|
+
generatorVersion: getPackageVersion(),
|
|
37
|
+
managedFiles: getManagedFileHashes(targetDirectory),
|
|
38
|
+
};
|
|
39
|
+
const metadataPath = node_path_1.default.join(modoockDirectory, constants_1.METADATA_FILE);
|
|
40
|
+
node_fs_1.default.writeFileSync(metadataPath, JSON.stringify(metadata, null, 2) + "\n");
|
|
41
|
+
}
|
|
42
|
+
function readModoockMetadata(targetDirectory) {
|
|
43
|
+
const metadataPath = node_path_1.default.join(targetDirectory, constants_1.MODOOCK_DIRECTORY, constants_1.METADATA_FILE);
|
|
44
|
+
if (!node_fs_1.default.existsSync(metadataPath)) {
|
|
45
|
+
throw new Error("This project was not created by create-modoock-app or is missing Modoock metadata.");
|
|
46
|
+
}
|
|
47
|
+
return JSON.parse(node_fs_1.default.readFileSync(metadataPath, "utf8"));
|
|
48
|
+
}
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getLatestVersion = getLatestVersion;
|
|
4
|
+
exports.getLatestModoockDependencies = getLatestModoockDependencies;
|
|
5
|
+
exports.installDependencies = installDependencies;
|
|
6
|
+
const node_child_process_1 = require("node:child_process");
|
|
7
|
+
const constants_1 = require("../constants");
|
|
8
|
+
function getLatestVersion(packageName) {
|
|
9
|
+
try {
|
|
10
|
+
const version = (0, node_child_process_1.execSync)(`npm view ${packageName} version`, {
|
|
11
|
+
encoding: "utf8",
|
|
12
|
+
stdio: [
|
|
13
|
+
"ignore",
|
|
14
|
+
"pipe",
|
|
15
|
+
"pipe",
|
|
16
|
+
],
|
|
17
|
+
}).trim();
|
|
18
|
+
if (!version) {
|
|
19
|
+
throw new Error();
|
|
20
|
+
}
|
|
21
|
+
return version;
|
|
22
|
+
}
|
|
23
|
+
catch {
|
|
24
|
+
throw new Error(`Could not resolve the latest stable version of ${packageName}.`);
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
function getLatestModoockDependencies() {
|
|
28
|
+
const dependencies = {};
|
|
29
|
+
for (const packageName of constants_1.MODOOCK_PACKAGES) {
|
|
30
|
+
console.log(`Resolving latest ${packageName}...`);
|
|
31
|
+
const version = getLatestVersion(packageName);
|
|
32
|
+
dependencies[packageName] = version;
|
|
33
|
+
console.log(` ${packageName}@${version}`);
|
|
34
|
+
}
|
|
35
|
+
return dependencies;
|
|
36
|
+
}
|
|
37
|
+
function installDependencies(targetDirectory) {
|
|
38
|
+
console.log("");
|
|
39
|
+
console.log("Installing dependencies...");
|
|
40
|
+
try {
|
|
41
|
+
(0, node_child_process_1.execSync)("npm install", {
|
|
42
|
+
cwd: targetDirectory,
|
|
43
|
+
stdio: "inherit",
|
|
44
|
+
});
|
|
45
|
+
}
|
|
46
|
+
catch {
|
|
47
|
+
throw new Error("Failed to install project dependencies.");
|
|
48
|
+
}
|
|
49
|
+
}
|