create-ncblock 0.0.3 → 0.0.5

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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "create-ncblock",
3
- "version": "0.0.3",
3
+ "version": "0.0.5",
4
4
  "description": "Create a Notion custom view block project.",
5
5
  "type": "module",
6
6
  "bin": {
@@ -0,0 +1,101 @@
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
+ }
@@ -12,6 +12,7 @@ import {
12
12
  } from "fs"
13
13
  import { tmpdir } from "os"
14
14
  import { resolve } from "path"
15
+ import { cleanDist, stagePublishDir } from "./compile-sdk"
15
16
 
16
17
  type StoredTemplateMetadata = {
17
18
  title?: string
@@ -145,15 +146,19 @@ function readSdkVersion(root: string): string {
145
146
 
146
147
  function packLocalSdk(root: string, dest: string): string {
147
148
  const sdkDir = resolve(root, "sdk")
148
- const tgzOutput = execSync("npm pack --pack-destination .", {
149
- cwd: sdkDir,
150
- encoding: "utf-8",
151
- }).trim()
152
- const tgz = tgzOutput.split(/\r?\n/).at(-1) ?? tgzOutput
153
- const tgzPath = resolve(sdkDir, tgz)
154
- copyFileSync(tgzPath, resolve(dest, tgz))
155
- rmSync(tgzPath, { force: true })
156
- return `file:./${tgz}`
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
+ }
157
162
  }
158
163
 
159
164
  function copyDirectoryContents(