@smartcat/sanity-functions 1.1.0 → 1.2.1

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.
Files changed (3) hide show
  1. package/bin/cli.js +76 -8
  2. package/bin/cli.ts +24 -5
  3. package/package.json +10 -2
package/bin/cli.js CHANGED
@@ -2,8 +2,61 @@
2
2
 
3
3
  // bin/cli.ts
4
4
  import { createRequire } from "node:module";
5
- import { existsSync, mkdirSync, readFileSync, writeFileSync } from "node:fs";
5
+ import { existsSync, mkdirSync, readdirSync, readFileSync, writeFileSync } from "node:fs";
6
+
7
+ // src/cli/init.ts
6
8
  import path from "node:path";
9
+ function init(deps) {
10
+ const { packageRoot: packageRoot2, targetDir, force, fs, log } = deps;
11
+ if (fs.existsSync(targetDir) && fs.readdirSync(targetDir).length > 0 && !force) {
12
+ log(`Target ${targetDir} is not empty. Re-run with --force to overwrite.`);
13
+ return { code: 1, files: [] };
14
+ }
15
+ const funcPkg = JSON.parse(fs.readFileSync(path.join(packageRoot2, "package.json"), "utf8"));
16
+ const names = fs.readdirSync(path.join(packageRoot2, "functions")).filter((n) => fs.existsSync(path.join(packageRoot2, "functions", n, "index.ts")));
17
+ const files = [];
18
+ const write = (rel, content) => {
19
+ const abs = path.join(targetDir, rel);
20
+ fs.mkdirSync(path.dirname(abs), { recursive: true });
21
+ fs.writeFileSync(abs, content);
22
+ files.push(rel);
23
+ };
24
+ const copy = (rel) => write(rel, fs.readFileSync(path.join(packageRoot2, rel), "utf8"));
25
+ copy("sanity.blueprint.ts");
26
+ for (const name of names) {
27
+ copy(`functions/${name}/index.ts`);
28
+ }
29
+ const d = funcPkg.dependencies;
30
+ write(
31
+ "package.json",
32
+ `${JSON.stringify(
33
+ {
34
+ name: "smartcat-functions-deploy",
35
+ private: true,
36
+ type: "module",
37
+ dependencies: {
38
+ "@smartcat/sanity-plugin": `^${funcPkg.version}`,
39
+ "@sanity/blueprints": d["@sanity/blueprints"],
40
+ "@sanity/functions": d["@sanity/functions"],
41
+ "@sanity/client": d["@sanity/client"]
42
+ }
43
+ },
44
+ null,
45
+ 2
46
+ )}
47
+ `
48
+ );
49
+ write(".gitignore", "node_modules\n");
50
+ log(`Scaffolded ${files.length} files into ${targetDir}`);
51
+ log("Next:");
52
+ log(` cd ${targetDir} && npm install`);
53
+ log(" # source your Smartcat .env (SMARTCAT_API_SERVER / _WORKSPACE_ID / _API_KEY) and run `sanity login`");
54
+ log(" npx sanity blueprints deploy --project-id <your-project-id>");
55
+ return { code: 0, files };
56
+ }
57
+
58
+ // bin/cli.ts
59
+ import path2 from "node:path";
7
60
  import { fileURLToPath } from "node:url";
8
61
  import { spawn } from "node:child_process";
9
62
  import { createInterface } from "node:readline/promises";
@@ -85,16 +138,19 @@ async function deploy(deps) {
85
138
  }
86
139
 
87
140
  // bin/cli.ts
88
- var packageRoot = path.resolve(path.dirname(fileURLToPath(import.meta.url)), "..");
141
+ var packageRoot = path2.resolve(path2.dirname(fileURLToPath(import.meta.url)), "..");
89
142
  var cwd = process.cwd();
90
- var smartcatConfigPath = path.join(cwd, ".smartcat", "credentials.json");
143
+ var smartcatConfigPath = path2.join(cwd, ".smartcat", "credentials.json");
91
144
  function parseArgs(argv) {
92
145
  const rest = [...argv];
93
146
  const command = rest[0] && !rest[0].startsWith("-") ? rest.shift() : void 0;
94
147
  const out = { command };
148
+ if (command === "init" && rest[0] && !rest[0].startsWith("-")) out.dir = rest.shift();
95
149
  for (let i = 0; i < rest.length; i++) {
96
150
  if (rest[i] === "--project" && rest[i + 1] !== void 0) out.project = rest[++i];
97
151
  else if (rest[i] === "--stack" && rest[i + 1] !== void 0) out.stack = rest[++i];
152
+ else if (rest[i] === "--dir" && rest[i + 1] !== void 0) out.dir = rest[++i];
153
+ else if (rest[i] === "--force") out.force = true;
98
154
  }
99
155
  return out;
100
156
  }
@@ -107,7 +163,7 @@ function readSmartcatConfig() {
107
163
  }
108
164
  }
109
165
  function writeSmartcatConfig(creds) {
110
- mkdirSync(path.dirname(smartcatConfigPath), { recursive: true });
166
+ mkdirSync(path2.dirname(smartcatConfigPath), { recursive: true });
111
167
  writeFileSync(smartcatConfigPath, `${JSON.stringify(creds, null, 2)}
112
168
  `);
113
169
  }
@@ -127,7 +183,7 @@ async function promptSmartcatCreds() {
127
183
  }
128
184
  function readSanityCliConfig() {
129
185
  for (const file of ["sanity.cli.ts", "sanity.config.ts"]) {
130
- const filePath = path.join(cwd, file);
186
+ const filePath = path2.join(cwd, file);
131
187
  if (!existsSync(filePath)) continue;
132
188
  const match = readFileSync(filePath, "utf8").match(/projectId\s*:\s*['"]([a-z0-9]+)['"]/i);
133
189
  if (match) return match[1];
@@ -168,7 +224,7 @@ async function fetchDeployedVersion(opts) {
168
224
  }
169
225
  function readInstalledPluginVersion() {
170
226
  try {
171
- const require2 = createRequire(path.join(cwd, "package.json"));
227
+ const require2 = createRequire(path2.join(cwd, "package.json"));
172
228
  const pkgPath = require2.resolve("@smartcat/sanity-plugin/package.json");
173
229
  const pkg = JSON.parse(readFileSync(pkgPath, "utf8"));
174
230
  return typeof pkg.version === "string" ? pkg.version : null;
@@ -177,10 +233,22 @@ function readInstalledPluginVersion() {
177
233
  }
178
234
  }
179
235
  async function main() {
180
- const { command, project, stack } = parseArgs(process.argv.slice(2));
236
+ const { command, project, stack, dir, force } = parseArgs(process.argv.slice(2));
237
+ if (command === "init") {
238
+ const target = path2.resolve(cwd, dir ?? "smartcat-functions");
239
+ const result2 = init({
240
+ packageRoot,
241
+ targetDir: target,
242
+ force: Boolean(force),
243
+ fs: { existsSync, readdirSync, readFileSync, mkdirSync, writeFileSync },
244
+ log: (line) => console.log(line)
245
+ });
246
+ process.exitCode = result2.code;
247
+ return;
248
+ }
181
249
  if (command !== "deploy") {
182
250
  console.error(
183
- 'Usage: smartcat-sanity-functions deploy [--project <id>] [--stack <name>, defaults to "production"]'
251
+ 'Usage:\n smartcat-sanity-functions deploy [--project <id>] [--stack <name>, defaults to "production"]\n smartcat-sanity-functions init [<dir>] [--force] scaffold an ejectable blueprint folder'
184
252
  );
185
253
  process.exitCode = 1;
186
254
  return;
package/bin/cli.ts CHANGED
@@ -1,5 +1,6 @@
1
1
  import {createRequire} from 'node:module'
2
- import {existsSync, mkdirSync, readFileSync, writeFileSync} from 'node:fs'
2
+ import {existsSync, mkdirSync, readdirSync, readFileSync, writeFileSync} from 'node:fs'
3
+ import {init} from '../src/cli/init'
3
4
  import path from 'node:path'
4
5
  import {fileURLToPath} from 'node:url'
5
6
  import {spawn} from 'node:child_process'
@@ -13,13 +14,16 @@ const packageRoot = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '
13
14
  const cwd = process.cwd()
14
15
  const smartcatConfigPath = path.join(cwd, '.smartcat', 'credentials.json')
15
16
 
16
- function parseArgs(argv: string[]): {command?: string; project?: string; stack?: string} {
17
+ function parseArgs(argv: string[]): {command?: string; project?: string; stack?: string; dir?: string; force?: boolean} {
17
18
  const rest = [...argv]
18
19
  const command = rest[0] && !rest[0].startsWith('-') ? rest.shift() : undefined
19
- const out: {command?: string; project?: string; stack?: string} = {command}
20
+ const out: {command?: string; project?: string; stack?: string; dir?: string; force?: boolean} = {command}
21
+ if (command === 'init' && rest[0] && !rest[0].startsWith('-')) out.dir = rest.shift()
20
22
  for (let i = 0; i < rest.length; i++) {
21
23
  if (rest[i] === '--project' && rest[i + 1] !== undefined) out.project = rest[++i]
22
24
  else if (rest[i] === '--stack' && rest[i + 1] !== undefined) out.stack = rest[++i]
25
+ else if (rest[i] === '--dir' && rest[i + 1] !== undefined) out.dir = rest[++i]
26
+ else if (rest[i] === '--force') out.force = true
23
27
  }
24
28
  return out
25
29
  }
@@ -128,11 +132,26 @@ function readInstalledPluginVersion(): string | null {
128
132
  }
129
133
 
130
134
  async function main(): Promise<void> {
131
- const {command, project, stack} = parseArgs(process.argv.slice(2))
135
+ const {command, project, stack, dir, force} = parseArgs(process.argv.slice(2))
136
+
137
+ if (command === 'init') {
138
+ const target = path.resolve(cwd, dir ?? 'smartcat-functions')
139
+ const result = init({
140
+ packageRoot,
141
+ targetDir: target,
142
+ force: Boolean(force),
143
+ fs: {existsSync, readdirSync, readFileSync, mkdirSync, writeFileSync},
144
+ log: (line) => console.log(line),
145
+ })
146
+ process.exitCode = result.code
147
+ return
148
+ }
132
149
 
133
150
  if (command !== 'deploy') {
134
151
  console.error(
135
- 'Usage: smartcat-sanity-functions deploy [--project <id>] [--stack <name>, defaults to "production"]',
152
+ 'Usage:\n' +
153
+ ' smartcat-sanity-functions deploy [--project <id>] [--stack <name>, defaults to "production"]\n' +
154
+ ' smartcat-sanity-functions init [<dir>] [--force] scaffold an ejectable blueprint folder',
136
155
  )
137
156
  process.exitCode = 1
138
157
  return
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@smartcat/sanity-functions",
3
- "version": "1.1.0",
3
+ "version": "1.2.1",
4
4
  "private": false,
5
5
  "license": "MIT",
6
6
  "type": "module",
@@ -12,8 +12,16 @@
12
12
  "sanity.blueprint.ts",
13
13
  "bin"
14
14
  ],
15
+ "exports": {
16
+ "./blueprint": "./sanity.blueprint.ts",
17
+ "./functions/smartcat-export": "./functions/smartcat-export/index.ts",
18
+ "./functions/smartcat-import": "./functions/smartcat-import/index.ts",
19
+ "./functions/smartcat-progress": "./functions/smartcat-progress/index.ts",
20
+ "./functions/smartcat-templates": "./functions/smartcat-templates/index.ts",
21
+ "./package.json": "./package.json"
22
+ },
15
23
  "dependencies": {
16
- "@smartcat/sanity-plugin": "1.1.0",
24
+ "@smartcat/sanity-plugin": "1.2.1",
17
25
  "@sanity/blueprints": "^0.20.1",
18
26
  "@sanity/functions": "^1.3.1",
19
27
  "@sanity/client": "^7.22.1"