@project-ajax/create 0.0.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/CODEOWNERS +1 -0
- package/README.md +9 -0
- package/package.json +17 -0
- package/src/index.js +200 -0
package/CODEOWNERS
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
* @makenotion/ajax-reviewers
|
package/README.md
ADDED
package/package.json
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@project-ajax/create",
|
|
3
|
+
"version": "0.0.0",
|
|
4
|
+
"description": "Initialize a new Notion Project Ajax extensions repo.",
|
|
5
|
+
"bin": {
|
|
6
|
+
"create-ajax": "src/index.js"
|
|
7
|
+
},
|
|
8
|
+
"publishConfig": {
|
|
9
|
+
"access": "public"
|
|
10
|
+
},
|
|
11
|
+
"dependencies": {
|
|
12
|
+
"chalk": "^5.3.0",
|
|
13
|
+
"execa": "^8.0.0",
|
|
14
|
+
"ora": "^8.0.1",
|
|
15
|
+
"prompts": "^2.4.2"
|
|
16
|
+
}
|
|
17
|
+
}
|
package/src/index.js
ADDED
|
@@ -0,0 +1,200 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
import fs from "node:fs";
|
|
4
|
+
import os from "node:os";
|
|
5
|
+
import path from "node:path";
|
|
6
|
+
import chalk from "chalk";
|
|
7
|
+
import { execa } from "execa";
|
|
8
|
+
import ora from "ora";
|
|
9
|
+
import prompts from "prompts";
|
|
10
|
+
|
|
11
|
+
run();
|
|
12
|
+
|
|
13
|
+
async function run() {
|
|
14
|
+
console.log(
|
|
15
|
+
chalk.bold.cyan("\n🚀 Create a new Project Ajax worker\n"),
|
|
16
|
+
);
|
|
17
|
+
|
|
18
|
+
const { directoryName, projectName } = await prompts([
|
|
19
|
+
{
|
|
20
|
+
type: "text",
|
|
21
|
+
name: "directoryName",
|
|
22
|
+
message: "Path to the new worker project:",
|
|
23
|
+
initial: ".",
|
|
24
|
+
},
|
|
25
|
+
{
|
|
26
|
+
type: "text",
|
|
27
|
+
name: "projectName",
|
|
28
|
+
message: "Project name:",
|
|
29
|
+
initial: "my-worker",
|
|
30
|
+
},
|
|
31
|
+
]);
|
|
32
|
+
|
|
33
|
+
if (!directoryName || !projectName) {
|
|
34
|
+
console.log(chalk.red("Cancelled."));
|
|
35
|
+
process.exit(1);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
const spinner = ora("Setting up template...").start();
|
|
39
|
+
|
|
40
|
+
let tempDir;
|
|
41
|
+
|
|
42
|
+
try {
|
|
43
|
+
// Create a temporary directory for sparse checkout
|
|
44
|
+
tempDir = fs.mkdtempSync(path.join(os.tmpdir(), "project-ajax-"));
|
|
45
|
+
|
|
46
|
+
spinner.text = "Fetching template from repository...";
|
|
47
|
+
await pullCode(tempDir);
|
|
48
|
+
|
|
49
|
+
spinner.text = "Preparing destination...";
|
|
50
|
+
const destPath = prepareDestination(directoryName);
|
|
51
|
+
|
|
52
|
+
spinner.text = "Copying template files...";
|
|
53
|
+
copyTemplate(tempDir, destPath);
|
|
54
|
+
|
|
55
|
+
spinner.text = "Customizing package.json...";
|
|
56
|
+
customizePackageJson(destPath, projectName);
|
|
57
|
+
|
|
58
|
+
spinner.succeed(chalk.green("Worker project created successfully!"));
|
|
59
|
+
|
|
60
|
+
printNextSteps(directoryName);
|
|
61
|
+
} catch (err) {
|
|
62
|
+
spinner.fail("Failed to create project.");
|
|
63
|
+
console.error(chalk.red(`\n${err.message}`));
|
|
64
|
+
|
|
65
|
+
if (
|
|
66
|
+
err.message.includes("Permission denied") ||
|
|
67
|
+
err.message.includes("publickey")
|
|
68
|
+
) {
|
|
69
|
+
console.log(
|
|
70
|
+
chalk.yellow("\n⚠️ SSH authentication failed. Make sure you have:"),
|
|
71
|
+
);
|
|
72
|
+
console.log(" 1. SSH keys set up with GitHub");
|
|
73
|
+
console.log(" 2. Added your SSH key to your GitHub account");
|
|
74
|
+
console.log(
|
|
75
|
+
" 3. See: https://docs.github.com/en/authentication/connecting-to-github-with-ssh",
|
|
76
|
+
);
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
process.exit(1);
|
|
80
|
+
} finally {
|
|
81
|
+
// Clean up the temporary directory.
|
|
82
|
+
if (tempDir) {
|
|
83
|
+
try {
|
|
84
|
+
fs.rmSync(tempDir, { recursive: true, force: true });
|
|
85
|
+
} catch {
|
|
86
|
+
// Ignore cleanup errors.
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
};
|
|
91
|
+
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* Pulls the template code from the repository using sparse checkout
|
|
95
|
+
* @param tempDir Temporary directory path
|
|
96
|
+
*/
|
|
97
|
+
async function pullCode(tempDir) {
|
|
98
|
+
// Initialize git repo with sparse checkout
|
|
99
|
+
await execa("git", ["init"], { cwd: tempDir });
|
|
100
|
+
await execa(
|
|
101
|
+
"git",
|
|
102
|
+
["remote", "add", "origin", "git@github.com:makenotion/project-ajax.git"],
|
|
103
|
+
{ cwd: tempDir },
|
|
104
|
+
);
|
|
105
|
+
await execa("git", ["config", "core.sparseCheckout", "true"], {
|
|
106
|
+
cwd: tempDir,
|
|
107
|
+
});
|
|
108
|
+
|
|
109
|
+
// Configure sparse checkout to only get packages/template
|
|
110
|
+
const sparseCheckoutPath = path.join(
|
|
111
|
+
tempDir,
|
|
112
|
+
".git",
|
|
113
|
+
"info",
|
|
114
|
+
"sparse-checkout",
|
|
115
|
+
);
|
|
116
|
+
fs.writeFileSync(sparseCheckoutPath, "packages/template/*\n");
|
|
117
|
+
|
|
118
|
+
// Pull only the template directory
|
|
119
|
+
await execa("git", ["pull", "--depth=1", "origin", "main"], {
|
|
120
|
+
cwd: tempDir,
|
|
121
|
+
});
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
/**
|
|
125
|
+
* Prepares and validates the destination directory
|
|
126
|
+
* @param repoName Project name or "." for current directory
|
|
127
|
+
* @returns The resolved destination path
|
|
128
|
+
*/
|
|
129
|
+
function prepareDestination(repoName) {
|
|
130
|
+
const destPath =
|
|
131
|
+
repoName === "." ? process.cwd() : path.resolve(process.cwd(), repoName);
|
|
132
|
+
|
|
133
|
+
// Check if destination has any files
|
|
134
|
+
if (fs.existsSync(destPath)) {
|
|
135
|
+
const files = fs.readdirSync(destPath);
|
|
136
|
+
if (files.length > 0) {
|
|
137
|
+
throw new Error(
|
|
138
|
+
`Destination directory "${repoName}" is not empty. Please use an empty directory.`,
|
|
139
|
+
);
|
|
140
|
+
}
|
|
141
|
+
} else if (repoName !== ".") {
|
|
142
|
+
// Create destination directory if it doesn't exist
|
|
143
|
+
fs.mkdirSync(destPath, { recursive: true });
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
return destPath;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
/**
|
|
150
|
+
* Copies template files from temporary directory to destination
|
|
151
|
+
* @param tempDir Temporary directory path
|
|
152
|
+
* @param destPath Destination directory path
|
|
153
|
+
*/
|
|
154
|
+
function copyTemplate(tempDir, destPath) {
|
|
155
|
+
const templatePath = path.join(tempDir, "packages", "template");
|
|
156
|
+
const templateFiles = fs.readdirSync(templatePath);
|
|
157
|
+
|
|
158
|
+
for (const file of templateFiles) {
|
|
159
|
+
const srcPath = path.join(templatePath, file);
|
|
160
|
+
const destFilePath = path.join(destPath, file);
|
|
161
|
+
fs.cpSync(srcPath, destFilePath, { recursive: true });
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
/**
|
|
166
|
+
* Updates package.json with the project name
|
|
167
|
+
* @param destPath Destination directory path
|
|
168
|
+
* @param projectName Name for the project
|
|
169
|
+
*/
|
|
170
|
+
function customizePackageJson(destPath, projectName) {
|
|
171
|
+
const packageJsonPath = path.join(destPath, "package.json");
|
|
172
|
+
const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, "utf8"));
|
|
173
|
+
packageJson.name = projectName;
|
|
174
|
+
fs.writeFileSync(
|
|
175
|
+
packageJsonPath,
|
|
176
|
+
`${JSON.stringify(packageJson, null, 2)}\n`,
|
|
177
|
+
);
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
/**
|
|
181
|
+
* Prints next steps instructions
|
|
182
|
+
* @param directoryName Directory name or "." for current directory
|
|
183
|
+
*/
|
|
184
|
+
function printNextSteps(directoryName) {
|
|
185
|
+
console.log(chalk.cyan(`\n✨ Next steps:`));
|
|
186
|
+
if (directoryName === ".") {
|
|
187
|
+
console.log(`
|
|
188
|
+
${chalk.bold("npm install")}
|
|
189
|
+
${chalk.bold("npx workers auth login")}
|
|
190
|
+
${chalk.bold("npx workers deploy")}
|
|
191
|
+
`);
|
|
192
|
+
} else {
|
|
193
|
+
console.log(`
|
|
194
|
+
${chalk.bold(`cd ${directoryName}`)}
|
|
195
|
+
${chalk.bold("npm install")}
|
|
196
|
+
${chalk.bold("npx workers auth login")}
|
|
197
|
+
${chalk.bold("npx workers deploy")}
|
|
198
|
+
`);
|
|
199
|
+
}
|
|
200
|
+
}
|