create-ncblock 0.0.5 → 0.0.6
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/scripts/init.ts +28 -0
- package/scripts/utils/templates.ts +7 -79
- package/sdk-version.json +1 -1
- package/scripts/utils/compile-sdk.ts +0 -101
package/package.json
CHANGED
package/scripts/init.ts
CHANGED
|
@@ -56,6 +56,8 @@ function parseArgs(argv: string[]) {
|
|
|
56
56
|
args.install = false
|
|
57
57
|
} else if (arg === "--force") {
|
|
58
58
|
args.force = true
|
|
59
|
+
} else if (arg === "--collection") {
|
|
60
|
+
args.collection = argv[++i]
|
|
59
61
|
} else if (!arg.startsWith("-")) {
|
|
60
62
|
positional.push(arg)
|
|
61
63
|
}
|
|
@@ -514,6 +516,32 @@ async function main() {
|
|
|
514
516
|
const installArgs = pm === "pnpm" ? "install --ignore-workspace" : "install"
|
|
515
517
|
execSync(`${pm} ${installArgs}`, { cwd: dest, stdio: "inherit" })
|
|
516
518
|
step("Installed dependencies")
|
|
519
|
+
|
|
520
|
+
// Ask for a collection URL / block ID to populate custom_blocks.json.
|
|
521
|
+
// Default to BLOCK_ID env var if set (the deploy flow already provides it).
|
|
522
|
+
const blockOrCollectionDefault = process.env.BLOCK_ID || "skip"
|
|
523
|
+
const blockOrCollectionId = (
|
|
524
|
+
await ask(
|
|
525
|
+
"Collection URL or block ID:",
|
|
526
|
+
blockOrCollectionDefault,
|
|
527
|
+
args.collection as string | undefined,
|
|
528
|
+
)
|
|
529
|
+
).trim()
|
|
530
|
+
if (blockOrCollectionId && blockOrCollectionId !== "skip") {
|
|
531
|
+
console.log("")
|
|
532
|
+
try {
|
|
533
|
+
execSync(`npx ncblock pull manifest ${blockOrCollectionId}`, {
|
|
534
|
+
cwd: dest,
|
|
535
|
+
stdio: "inherit",
|
|
536
|
+
})
|
|
537
|
+
step("Updated custom_blocks.json from collection schema")
|
|
538
|
+
} catch {
|
|
539
|
+
console.log(
|
|
540
|
+
` ${c.yellow}Could not fetch collection schema. You can run this later:${c.reset}`,
|
|
541
|
+
)
|
|
542
|
+
console.log(` npx ncblock pull manifest ${blockOrCollectionId}\n`)
|
|
543
|
+
}
|
|
544
|
+
}
|
|
517
545
|
}
|
|
518
546
|
|
|
519
547
|
const blockId = process.env.BLOCK_ID
|
|
@@ -1,18 +1,13 @@
|
|
|
1
|
-
import { execSync } from "child_process"
|
|
2
1
|
import {
|
|
3
2
|
copyFileSync,
|
|
4
3
|
existsSync,
|
|
5
4
|
mkdirSync,
|
|
6
|
-
mkdtempSync,
|
|
7
5
|
readdirSync,
|
|
8
6
|
readFileSync,
|
|
9
|
-
rmSync,
|
|
10
7
|
statSync,
|
|
11
8
|
writeFileSync,
|
|
12
9
|
} from "fs"
|
|
13
|
-
import { tmpdir } from "os"
|
|
14
10
|
import { resolve } from "path"
|
|
15
|
-
import { cleanDist, stagePublishDir } from "./compile-sdk"
|
|
16
11
|
|
|
17
12
|
type StoredTemplateMetadata = {
|
|
18
13
|
title?: string
|
|
@@ -116,12 +111,12 @@ type ScaffoldTemplateOptions = {
|
|
|
116
111
|
name: string
|
|
117
112
|
overwrite?: boolean
|
|
118
113
|
/**
|
|
119
|
-
*
|
|
120
|
-
* scaffolded
|
|
121
|
-
*
|
|
122
|
-
*
|
|
114
|
+
* Optional override for the `ncblock` dependency value written into the
|
|
115
|
+
* scaffolded `package.json`. Defaults to `^<version>` resolved from the
|
|
116
|
+
* SDK version metadata. Tests pass a `file:./<tgz>` here to point
|
|
117
|
+
* scaffolded projects at a locally-built SDK tarball.
|
|
123
118
|
*/
|
|
124
|
-
|
|
119
|
+
sdkDep?: string
|
|
125
120
|
}
|
|
126
121
|
|
|
127
122
|
const SDK_PACKAGE_NAME = "ncblock"
|
|
@@ -144,23 +139,6 @@ function readSdkVersion(root: string): string {
|
|
|
144
139
|
return sdkPkg.version
|
|
145
140
|
}
|
|
146
141
|
|
|
147
|
-
function packLocalSdk(root: string, dest: string): string {
|
|
148
|
-
const sdkDir = resolve(root, "sdk")
|
|
149
|
-
const publishDir = stagePublishDir(sdkDir)
|
|
150
|
-
try {
|
|
151
|
-
const tgzOutput = execSync("npm pack --pack-destination .", {
|
|
152
|
-
cwd: publishDir,
|
|
153
|
-
encoding: "utf-8",
|
|
154
|
-
}).trim()
|
|
155
|
-
const tgz = tgzOutput.split(/\r?\n/).at(-1) ?? tgzOutput
|
|
156
|
-
copyFileSync(resolve(publishDir, tgz), resolve(dest, tgz))
|
|
157
|
-
return `file:./${tgz}`
|
|
158
|
-
} finally {
|
|
159
|
-
rmSync(publishDir, { recursive: true, force: true })
|
|
160
|
-
cleanDist(sdkDir)
|
|
161
|
-
}
|
|
162
|
-
}
|
|
163
|
-
|
|
164
142
|
function copyDirectoryContents(
|
|
165
143
|
sourceDir: string,
|
|
166
144
|
targetDir: string,
|
|
@@ -205,7 +183,7 @@ export function scaffoldTemplate({
|
|
|
205
183
|
dest,
|
|
206
184
|
name,
|
|
207
185
|
overwrite = false,
|
|
208
|
-
|
|
186
|
+
sdkDep,
|
|
209
187
|
}: ScaffoldTemplateOptions) {
|
|
210
188
|
copyDirectoryContents(templateDir, dest, {
|
|
211
189
|
overwrite,
|
|
@@ -242,57 +220,7 @@ export function scaffoldTemplate({
|
|
|
242
220
|
pkg.name = name
|
|
243
221
|
delete pkg.notionCustomTemplate
|
|
244
222
|
if (pkg.dependencies?.[SDK_PACKAGE_NAME]) {
|
|
245
|
-
pkg.dependencies[SDK_PACKAGE_NAME] =
|
|
246
|
-
? packLocalSdk(root, dest)
|
|
247
|
-
: `^${readSdkVersion(root)}`
|
|
223
|
+
pkg.dependencies[SDK_PACKAGE_NAME] = sdkDep ?? `^${readSdkVersion(root)}`
|
|
248
224
|
}
|
|
249
225
|
writeFileSync(pkgPath, JSON.stringify(pkg, null, "\t") + "\n")
|
|
250
226
|
}
|
|
251
|
-
|
|
252
|
-
export type ScaffoldedTemplate = {
|
|
253
|
-
template: string
|
|
254
|
-
dest: string
|
|
255
|
-
}
|
|
256
|
-
|
|
257
|
-
type ScaffoldAllOptions = {
|
|
258
|
-
root: string
|
|
259
|
-
templatesDir: string
|
|
260
|
-
pm: string
|
|
261
|
-
install?: boolean
|
|
262
|
-
localSdk?: boolean
|
|
263
|
-
}
|
|
264
|
-
|
|
265
|
-
export function scaffoldAllTemplates({
|
|
266
|
-
root,
|
|
267
|
-
templatesDir,
|
|
268
|
-
pm,
|
|
269
|
-
install = true,
|
|
270
|
-
localSdk = false,
|
|
271
|
-
}: ScaffoldAllOptions): ScaffoldedTemplate[] {
|
|
272
|
-
const templates = listTemplates(templatesDir)
|
|
273
|
-
const results: ScaffoldedTemplate[] = []
|
|
274
|
-
|
|
275
|
-
for (const template of templates) {
|
|
276
|
-
const dest = mkdtempSync(resolve(tmpdir(), `custom-${template}-`))
|
|
277
|
-
const templateDir = resolve(templatesDir, template)
|
|
278
|
-
|
|
279
|
-
console.log(`[${template}] Scaffolding → ${dest}`)
|
|
280
|
-
scaffoldTemplate({
|
|
281
|
-
root,
|
|
282
|
-
templateDir,
|
|
283
|
-
dest,
|
|
284
|
-
name: template,
|
|
285
|
-
overwrite: true,
|
|
286
|
-
localSdk,
|
|
287
|
-
})
|
|
288
|
-
|
|
289
|
-
if (install) {
|
|
290
|
-
console.log(`[${template}] Installing dependencies...`)
|
|
291
|
-
execSync(`${pm} install`, { cwd: dest, stdio: "inherit" })
|
|
292
|
-
}
|
|
293
|
-
|
|
294
|
-
results.push({ template, dest })
|
|
295
|
-
}
|
|
296
|
-
|
|
297
|
-
return results
|
|
298
|
-
}
|
package/sdk-version.json
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":"0.0.
|
|
1
|
+
{"version":"0.0.9"}
|
|
@@ -1,101 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Compiles the SDK and stages a publish-ready directory. Used by both
|
|
3
|
-
* `publish-sdk.ts` and `packLocalSdk` so the same compiled artifact is tested
|
|
4
|
-
* locally and shipped to npm.
|
|
5
|
-
*/
|
|
6
|
-
|
|
7
|
-
import { execSync } from "child_process"
|
|
8
|
-
import { cpSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from "fs"
|
|
9
|
-
import { tmpdir } from "os"
|
|
10
|
-
import { resolve } from "path"
|
|
11
|
-
|
|
12
|
-
function rewrite(value: string): string {
|
|
13
|
-
return value.replace("src/", "dist/").replace(/\.tsx?$/, ".js")
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
function rewriteDts(value: string): string {
|
|
17
|
-
return value.replace("src/", "dist/").replace(/\.tsx?$/, ".d.ts")
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
function isPrebuilt(value: string): boolean {
|
|
21
|
-
return /\.(js|d\.ts)$/.test(value)
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
export function createPublishPackageJson(sdkDir: string): string {
|
|
25
|
-
const pkg = JSON.parse(readFileSync(resolve(sdkDir, "package.json"), "utf-8"))
|
|
26
|
-
|
|
27
|
-
if (pkg.main && !isPrebuilt(pkg.main)) {
|
|
28
|
-
pkg.main = `./${rewrite(pkg.main)}`
|
|
29
|
-
}
|
|
30
|
-
if (pkg.types && !isPrebuilt(pkg.types)) {
|
|
31
|
-
pkg.types = `./${rewriteDts(pkg.types)}`
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
if (pkg.exports) {
|
|
35
|
-
for (const key of Object.keys(pkg.exports)) {
|
|
36
|
-
const entry = pkg.exports[key]
|
|
37
|
-
if (typeof entry === "string") {
|
|
38
|
-
if (!isPrebuilt(entry)) {
|
|
39
|
-
pkg.exports[key] = `./${rewrite(entry)}`
|
|
40
|
-
}
|
|
41
|
-
} else if (typeof entry === "object" && entry !== null) {
|
|
42
|
-
for (const condition of Object.keys(entry)) {
|
|
43
|
-
const target = entry[condition]
|
|
44
|
-
if (typeof target !== "string" || isPrebuilt(target)) {
|
|
45
|
-
continue
|
|
46
|
-
}
|
|
47
|
-
entry[condition] =
|
|
48
|
-
condition === "types"
|
|
49
|
-
? `./${rewriteDts(target)}`
|
|
50
|
-
: `./${rewrite(target)}`
|
|
51
|
-
}
|
|
52
|
-
}
|
|
53
|
-
}
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
return JSON.stringify(pkg, null, "\t") + "\n"
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
export function compileSdk(sdkDir: string) {
|
|
60
|
-
execSync("pnpm exec tsc --project tsconfig.json", {
|
|
61
|
-
cwd: sdkDir,
|
|
62
|
-
stdio: "inherit",
|
|
63
|
-
})
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
export function cleanDist(sdkDir: string) {
|
|
67
|
-
rmSync(resolve(sdkDir, "dist"), { recursive: true, force: true })
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
/**
|
|
71
|
-
* Compiles the SDK and stages a publish-ready directory: compiled `dist/`,
|
|
72
|
-
* rewritten `package.json`, and all publishable files. Returns the path to
|
|
73
|
-
* the staged directory — caller is responsible for cleanup.
|
|
74
|
-
*/
|
|
75
|
-
export function stagePublishDir(sdkDir: string): string {
|
|
76
|
-
cleanDist(sdkDir)
|
|
77
|
-
console.log("Compiling SDK...")
|
|
78
|
-
compileSdk(sdkDir)
|
|
79
|
-
|
|
80
|
-
const publishDir = mkdtempSync(resolve(tmpdir(), "custom-sdk-publish-"))
|
|
81
|
-
cpSync(sdkDir, publishDir, {
|
|
82
|
-
recursive: true,
|
|
83
|
-
filter: src => {
|
|
84
|
-
const rel = src.slice(sdkDir.length + 1)
|
|
85
|
-
if (!rel) {
|
|
86
|
-
return true
|
|
87
|
-
}
|
|
88
|
-
const top = rel.split("/")[0]
|
|
89
|
-
return top !== "node_modules" && top !== "test" && top !== "dist"
|
|
90
|
-
},
|
|
91
|
-
})
|
|
92
|
-
cpSync(resolve(sdkDir, "dist"), resolve(publishDir, "dist"), {
|
|
93
|
-
recursive: true,
|
|
94
|
-
})
|
|
95
|
-
writeFileSync(
|
|
96
|
-
resolve(publishDir, "package.json"),
|
|
97
|
-
createPublishPackageJson(sdkDir),
|
|
98
|
-
)
|
|
99
|
-
|
|
100
|
-
return publishDir
|
|
101
|
-
}
|