@take-out/cli 0.0.52 → 0.0.57

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 (56) hide show
  1. package/dist/cjs/cli.cjs +3 -1
  2. package/dist/cjs/cli.js +3 -1
  3. package/dist/cjs/cli.js.map +1 -1
  4. package/dist/cjs/commands/run-all.cjs +69 -0
  5. package/dist/cjs/commands/run-all.js +59 -0
  6. package/dist/cjs/commands/run-all.js.map +6 -0
  7. package/dist/cjs/commands/run.cjs +2 -2
  8. package/dist/cjs/commands/run.js +2 -2
  9. package/dist/cjs/commands/run.js.map +1 -1
  10. package/dist/cjs/commands/script.cjs +8 -83
  11. package/dist/cjs/commands/script.js +8 -85
  12. package/dist/cjs/commands/script.js.map +1 -1
  13. package/dist/cjs/utils/script-listing.cjs +9 -9
  14. package/dist/cjs/utils/script-listing.js +8 -8
  15. package/dist/cjs/utils/script-listing.js.map +1 -1
  16. package/dist/cjs/utils/script-utils.cjs +91 -0
  17. package/dist/cjs/utils/script-utils.js +87 -0
  18. package/dist/cjs/utils/script-utils.js.map +6 -0
  19. package/dist/cjs/utils/script-utils.native.js +126 -0
  20. package/dist/cjs/utils/script-utils.native.js.map +6 -0
  21. package/dist/esm/cli.js +3 -0
  22. package/dist/esm/cli.js.map +1 -1
  23. package/dist/esm/cli.mjs +3 -1
  24. package/dist/esm/cli.mjs.map +1 -1
  25. package/dist/esm/commands/run-all.js +46 -0
  26. package/dist/esm/commands/run-all.js.map +6 -0
  27. package/dist/esm/commands/run-all.mjs +46 -0
  28. package/dist/esm/commands/run-all.mjs.map +1 -0
  29. package/dist/esm/commands/run.js +2 -2
  30. package/dist/esm/commands/run.js.map +1 -1
  31. package/dist/esm/commands/run.mjs +2 -2
  32. package/dist/esm/commands/run.mjs.map +1 -1
  33. package/dist/esm/commands/script.js +6 -84
  34. package/dist/esm/commands/script.js.map +1 -1
  35. package/dist/esm/commands/script.mjs +7 -79
  36. package/dist/esm/commands/script.mjs.map +1 -1
  37. package/dist/esm/utils/script-listing.js +1 -5
  38. package/dist/esm/utils/script-listing.js.map +1 -1
  39. package/dist/esm/utils/script-listing.mjs +1 -1
  40. package/dist/esm/utils/script-listing.mjs.map +1 -1
  41. package/dist/esm/utils/script-utils.js +80 -0
  42. package/dist/esm/utils/script-utils.js.map +6 -0
  43. package/dist/esm/utils/script-utils.mjs +66 -0
  44. package/dist/esm/utils/script-utils.mjs.map +1 -0
  45. package/dist/esm/utils/script-utils.native.js +112 -0
  46. package/dist/esm/utils/script-utils.native.js.map +6 -0
  47. package/package.json +7 -7
  48. package/scripts/seed.ts +12 -4
  49. package/src/cli.ts +3 -0
  50. package/src/commands/run-all.ts +62 -0
  51. package/src/commands/run.ts +3 -2
  52. package/src/commands/script.ts +7 -157
  53. package/src/utils/script-listing.ts +1 -6
  54. package/src/utils/script-utils.ts +153 -0
  55. package/types/utils/script-utils.d.ts +13 -0
  56. package/types/utils/script-utils.d.ts.map +1 -0
@@ -4,20 +4,20 @@
4
4
 
5
5
  import {
6
6
  existsSync,
7
+ readFileSync,
7
8
  readdirSync,
8
9
  statSync,
9
- readFileSync,
10
10
  writeFileSync,
11
11
  mkdirSync,
12
12
  } from 'node:fs'
13
- import { homedir } from 'node:os'
14
- import { join, relative, dirname, basename, extname } from 'node:path'
13
+ import { join, relative, dirname, basename } from 'node:path'
15
14
  import { fileURLToPath } from 'node:url'
16
15
 
17
16
  import { defineCommand } from 'citty'
18
17
  import pc from 'picocolors'
19
18
 
20
19
  import { listAllScripts, listCategoryScripts } from '../utils/script-listing'
20
+ import { getLocalScriptsDir } from '../utils/script-utils'
21
21
  import {
22
22
  type FileToSync,
23
23
  compareFiles,
@@ -25,20 +25,6 @@ import {
25
25
  syncFileWithConfirmation,
26
26
  } from '../utils/sync'
27
27
 
28
- // cache configuration
29
- const CACHE_DIR = join(homedir(), '.takeout')
30
- const CACHE_FILE = join(CACHE_DIR, 'script-cache.json')
31
-
32
- interface ScriptMetadata {
33
- description?: string
34
- args?: string[]
35
- mtime?: number
36
- }
37
-
38
- interface ScriptCache {
39
- [path: string]: ScriptMetadata
40
- }
41
-
42
28
  // find scripts package root using import.meta.resolve
43
29
  function findScriptsPackageRoot(): string | null {
44
30
  try {
@@ -58,131 +44,6 @@ function findScriptsPackageRoot(): string | null {
58
44
  return null
59
45
  }
60
46
 
61
- // get local scripts directory
62
- export function getLocalScriptsDir(): string {
63
- return join(process.cwd(), 'scripts')
64
- }
65
-
66
- // extract metadata from script file
67
- function extractMetadata(filePath: string): ScriptMetadata {
68
- try {
69
- const content = readFileSync(filePath, 'utf-8')
70
- const lines = content.split('\n').slice(0, 20) // check first 20 lines
71
-
72
- const metadata: ScriptMetadata = {}
73
-
74
- for (const line of lines) {
75
- // look for @description comment
76
- const descMatch = line.match(/@description\s+(.+)/)
77
- if (descMatch && descMatch[1]) {
78
- metadata.description = descMatch[1].trim()
79
- }
80
-
81
- // look for @args comment
82
- const argsMatch = line.match(/@args\s+(.+)/)
83
- if (argsMatch && argsMatch[1]) {
84
- metadata.args = argsMatch[1].split(',').map((a) => a.trim())
85
- }
86
- }
87
-
88
- // add file modification time
89
- const stat = statSync(filePath)
90
- metadata.mtime = stat.mtimeMs
91
-
92
- return metadata
93
- } catch (err) {
94
- return {}
95
- }
96
- }
97
-
98
- // load or create cache
99
- function loadCache(): ScriptCache {
100
- try {
101
- if (existsSync(CACHE_FILE)) {
102
- return JSON.parse(readFileSync(CACHE_FILE, 'utf-8'))
103
- }
104
- } catch (err) {
105
- // ignore cache errors
106
- }
107
- return {}
108
- }
109
-
110
- // save cache
111
- function saveCache(cache: ScriptCache) {
112
- try {
113
- if (!existsSync(CACHE_DIR)) {
114
- mkdirSync(CACHE_DIR, { recursive: true })
115
- }
116
- writeFileSync(CACHE_FILE, JSON.stringify(cache, null, 2))
117
- } catch (err) {
118
- // ignore cache write errors
119
- }
120
- }
121
-
122
- // get metadata for a script (with caching)
123
- export function getScriptMetadata(filePath: string): ScriptMetadata {
124
- const cache = loadCache()
125
- const stat = statSync(filePath)
126
-
127
- // check if cache is valid
128
- if (cache[filePath] && cache[filePath].mtime === stat.mtimeMs) {
129
- return cache[filePath]
130
- }
131
-
132
- // extract fresh metadata
133
- const metadata = extractMetadata(filePath)
134
-
135
- // update cache
136
- cache[filePath] = metadata
137
- saveCache(cache)
138
-
139
- return metadata
140
- }
141
-
142
- // discover scripts in a directory
143
- export function discoverScripts(
144
- dir: string,
145
- baseDir: string = dir,
146
- depth: number = 0,
147
- maxDepth: number = 1
148
- ): Map<string, string> {
149
- const scripts = new Map<string, string>()
150
-
151
- if (!existsSync(dir)) {
152
- return scripts
153
- }
154
-
155
- try {
156
- const entries = readdirSync(dir)
157
-
158
- for (const entry of entries) {
159
- const fullPath = join(dir, entry)
160
- const stat = statSync(fullPath)
161
-
162
- if (stat.isDirectory()) {
163
- // skip helpers directories and limit depth to maxDepth
164
- if (entry === 'helpers' || depth >= maxDepth) {
165
- continue
166
- }
167
- // recursively discover in subdirectories
168
- const subScripts = discoverScripts(fullPath, baseDir, depth + 1, maxDepth)
169
- for (const [name, path] of subScripts) {
170
- scripts.set(name, path)
171
- }
172
- } else if (entry.endsWith('.ts') || entry.endsWith('.js')) {
173
- // create script name relative to base, normalize to forward slashes
174
- const relativePath = relative(baseDir, fullPath).split('\\').join('/')
175
- const scriptName = relativePath.replace(/\.(ts|js)$/, '')
176
- scripts.set(scriptName, fullPath)
177
- }
178
- }
179
- } catch (err) {
180
- // ignore directory read errors
181
- }
182
-
183
- return scripts
184
- }
185
-
186
47
  // find a script by name (checks both built-in and local)
187
48
  export function findScript(name: string): string | null {
188
49
  // normalize name: convert colons to slashes for consistency
@@ -275,29 +136,18 @@ const newCommand = defineCommand({
275
136
  * @args --verbose, --dry-run
276
137
  */
277
138
 
278
- import { parseArgs } from 'node:util'
139
+ import { args } from '@take-out/scripts/helpers/args'
279
140
 
280
- const { values } = parseArgs({
281
- args: process.argv.slice(2),
282
- options: {
283
- verbose: {
284
- type: 'boolean',
285
- short: 'v',
286
- },
287
- 'dry-run': {
288
- type: 'boolean',
289
- },
290
- },
291
- })
141
+ const { verbose, dryRun } = args('--verbose boolean --dry-run boolean')
292
142
 
293
143
  async function main() {
294
144
  console.info('Running ${basename(scriptPath)}...')
295
145
 
296
- if (values.verbose) {
146
+ if (verbose) {
297
147
  console.info('Verbose mode enabled')
298
148
  }
299
149
 
300
- if (values['dry-run']) {
150
+ if (dryRun) {
301
151
  console.info('Dry run mode - no changes will be made')
302
152
  }
303
153
 
@@ -8,12 +8,7 @@ import { fileURLToPath } from 'node:url'
8
8
 
9
9
  import pc from 'picocolors'
10
10
 
11
- // re-export needed functions from script command
12
- import {
13
- discoverScripts,
14
- getLocalScriptsDir,
15
- getScriptMetadata,
16
- } from '../commands/script'
11
+ import { discoverScripts, getLocalScriptsDir, getScriptMetadata } from './script-utils'
17
12
 
18
13
  // find scripts package root using import.meta.resolve
19
14
  function findScriptsPackageRoot(): string | null {
@@ -0,0 +1,153 @@
1
+ /**
2
+ * Core utility functions for script discovery and metadata
3
+ */
4
+
5
+ import {
6
+ existsSync,
7
+ readdirSync,
8
+ statSync,
9
+ readFileSync,
10
+ writeFileSync,
11
+ mkdirSync,
12
+ } from 'node:fs'
13
+ import { homedir } from 'node:os'
14
+ import { join, relative } from 'node:path'
15
+
16
+ // cache configuration
17
+ const CACHE_DIR = join(homedir(), '.takeout')
18
+ const CACHE_FILE = join(CACHE_DIR, 'script-cache.json')
19
+
20
+ interface ScriptMetadata {
21
+ description?: string
22
+ args?: string[]
23
+ mtime?: number
24
+ }
25
+
26
+ interface ScriptCache {
27
+ [path: string]: ScriptMetadata
28
+ }
29
+
30
+ // get local scripts directory
31
+ export function getLocalScriptsDir(): string {
32
+ return join(process.cwd(), 'scripts')
33
+ }
34
+
35
+ // extract metadata from script file
36
+ function extractMetadata(filePath: string): ScriptMetadata {
37
+ try {
38
+ const content = readFileSync(filePath, 'utf-8')
39
+ const lines = content.split('\n').slice(0, 20) // check first 20 lines
40
+
41
+ const metadata: ScriptMetadata = {}
42
+
43
+ for (const line of lines) {
44
+ // look for @description comment
45
+ const descMatch = line.match(/@description\s+(.+)/)
46
+ if (descMatch && descMatch[1]) {
47
+ metadata.description = descMatch[1].trim()
48
+ }
49
+
50
+ // look for @args comment
51
+ const argsMatch = line.match(/@args\s+(.+)/)
52
+ if (argsMatch && argsMatch[1]) {
53
+ metadata.args = argsMatch[1].split(',').map((a) => a.trim())
54
+ }
55
+ }
56
+
57
+ // add file modification time
58
+ const stat = statSync(filePath)
59
+ metadata.mtime = stat.mtimeMs
60
+
61
+ return metadata
62
+ } catch (err) {
63
+ return {}
64
+ }
65
+ }
66
+
67
+ // load or create cache
68
+ function loadCache(): ScriptCache {
69
+ try {
70
+ if (existsSync(CACHE_FILE)) {
71
+ return JSON.parse(readFileSync(CACHE_FILE, 'utf-8'))
72
+ }
73
+ } catch (err) {
74
+ // ignore cache errors
75
+ }
76
+ return {}
77
+ }
78
+
79
+ // save cache
80
+ function saveCache(cache: ScriptCache) {
81
+ try {
82
+ if (!existsSync(CACHE_DIR)) {
83
+ mkdirSync(CACHE_DIR, { recursive: true })
84
+ }
85
+ writeFileSync(CACHE_FILE, JSON.stringify(cache, null, 2))
86
+ } catch (err) {
87
+ // ignore cache write errors
88
+ }
89
+ }
90
+
91
+ // get metadata for a script (with caching)
92
+ export function getScriptMetadata(filePath: string): ScriptMetadata {
93
+ const cache = loadCache()
94
+ const stat = statSync(filePath)
95
+
96
+ // check if cache is valid
97
+ if (cache[filePath] && cache[filePath].mtime === stat.mtimeMs) {
98
+ return cache[filePath]
99
+ }
100
+
101
+ // extract fresh metadata
102
+ const metadata = extractMetadata(filePath)
103
+
104
+ // update cache
105
+ cache[filePath] = metadata
106
+ saveCache(cache)
107
+
108
+ return metadata
109
+ }
110
+
111
+ // discover scripts in a directory
112
+ export function discoverScripts(
113
+ dir: string,
114
+ baseDir: string = dir,
115
+ depth: number = 0,
116
+ maxDepth: number = 1
117
+ ): Map<string, string> {
118
+ const scripts = new Map<string, string>()
119
+
120
+ if (!existsSync(dir)) {
121
+ return scripts
122
+ }
123
+
124
+ try {
125
+ const entries = readdirSync(dir)
126
+
127
+ for (const entry of entries) {
128
+ const fullPath = join(dir, entry)
129
+ const stat = statSync(fullPath)
130
+
131
+ if (stat.isDirectory()) {
132
+ // skip helpers directories and limit depth to maxDepth
133
+ if (entry === 'helpers' || depth >= maxDepth) {
134
+ continue
135
+ }
136
+ // recursively discover in subdirectories
137
+ const subScripts = discoverScripts(fullPath, baseDir, depth + 1, maxDepth)
138
+ for (const [name, path] of subScripts) {
139
+ scripts.set(name, path)
140
+ }
141
+ } else if (entry.endsWith('.ts') || entry.endsWith('.js')) {
142
+ // create script name relative to base, normalize to forward slashes
143
+ const relativePath = relative(baseDir, fullPath).split('\\').join('/')
144
+ const scriptName = relativePath.replace(/\.(ts|js)$/, '')
145
+ scripts.set(scriptName, fullPath)
146
+ }
147
+ }
148
+ } catch (err) {
149
+ // ignore directory read errors
150
+ }
151
+
152
+ return scripts
153
+ }
@@ -0,0 +1,13 @@
1
+ /**
2
+ * Core utility functions for script discovery and metadata
3
+ */
4
+ interface ScriptMetadata {
5
+ description?: string;
6
+ args?: string[];
7
+ mtime?: number;
8
+ }
9
+ export declare function getLocalScriptsDir(): string;
10
+ export declare function getScriptMetadata(filePath: string): ScriptMetadata;
11
+ export declare function discoverScripts(dir: string, baseDir?: string, depth?: number, maxDepth?: number): Map<string, string>;
12
+ export {};
13
+ //# sourceMappingURL=script-utils.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"script-utils.d.ts","sourceRoot":"","sources":["../../src/utils/script-utils.ts"],"names":[],"mappings":"AAAA;;GAEG;AAiBH,UAAU,cAAc;IACtB,WAAW,CAAC,EAAE,MAAM,CAAA;IACpB,IAAI,CAAC,EAAE,MAAM,EAAE,CAAA;IACf,KAAK,CAAC,EAAE,MAAM,CAAA;CACf;AAOD,wBAAgB,kBAAkB,IAAI,MAAM,CAE3C;AA2DD,wBAAgB,iBAAiB,CAAC,QAAQ,EAAE,MAAM,GAAG,cAAc,CAiBlE;AAGD,wBAAgB,eAAe,CAC7B,GAAG,EAAE,MAAM,EACX,OAAO,GAAE,MAAY,EACrB,KAAK,GAAE,MAAU,EACjB,QAAQ,GAAE,MAAU,GACnB,GAAG,CAAC,MAAM,EAAE,MAAM,CAAC,CAoCrB"}