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