@garyr/pt-cli 1.1.1 → 1.3.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.
package/src/index.ts CHANGED
@@ -72,15 +72,31 @@ program
72
72
  });
73
73
 
74
74
  program
75
- .command('init [templateName] [destPath]')
76
- .description('Initialize a new project from a learned template')
75
+ .command('init [args...]')
76
+ .description('Initialize a new project from one or more learned templates')
77
77
  .option('-f, --file <jsonPath>', 'Initialize directly from a JSON template file without adding it to local config')
78
78
  .option('--skip-post-config', 'Skip running post-config tasks')
79
79
  .option('--dry-run', 'Show what would be created without making changes')
80
80
  .option('-y, --yes', 'Automatically answer yes to prompts')
81
81
  .option('--vars <variables>', 'Comma-separated key=value variables (e.g. key1=val1,key2=val2)')
82
- .action(async (typeName: string | undefined, destPath: string | undefined, options) => {
83
- await init(typeName, destPath, options);
82
+ .option('--collision <mode>', 'File collision resolution strategy (overwrite, newest)', 'overwrite')
83
+ .option('--json', 'Output result as JSON')
84
+ .action(async (args: string[], options) => {
85
+ try {
86
+ await init(args, options);
87
+ } catch (err: any) {
88
+ if (options.json) {
89
+ process.stdout.write(JSON.stringify({
90
+ status: 'error',
91
+ message: err.message || String(err)
92
+ }) + '\n', () => {
93
+ process.exit(1);
94
+ });
95
+ } else {
96
+ console.error(chalk.red(`Error: ${err.message || err}`));
97
+ process.exit(1);
98
+ }
99
+ }
84
100
  });
85
101
 
86
102
  program
package/src/substitute.ts CHANGED
@@ -50,7 +50,9 @@ export async function processCopyFiles(
50
50
  resolvedDest: string,
51
51
  template: TemplateConfig,
52
52
  variables: Record<string, string>,
53
- dryRun: boolean = false
53
+ dryRun: boolean = false,
54
+ collisionMode: 'overwrite' | 'newest' = 'overwrite',
55
+ silent: boolean = false
54
56
  ): Promise<void> {
55
57
  if (!template.copy_files) return;
56
58
 
@@ -59,7 +61,7 @@ export async function processCopyFiles(
59
61
  const destPath = path.join(resolvedDest, sanitizePath(copyFile.dest));
60
62
 
61
63
  if (!fs.existsSync(srcPath)) {
62
- console.warn(chalk.yellow(`Warning: ${copyFile.src} not found in template`));
64
+ if (!silent) console.warn(chalk.yellow(`Warning: ${copyFile.src} not found in template`));
63
65
  continue;
64
66
  }
65
67
 
@@ -67,7 +69,7 @@ export async function processCopyFiles(
67
69
  if (stat.isDirectory()) {
68
70
  // Recursive directory copy
69
71
  if (dryRun) {
70
- console.log(chalk.gray(` [DRY RUN] Would recursively copy directory ${copyFile.src} → ${copyFile.dest}`));
72
+ if (!silent) console.log(chalk.gray(` [DRY RUN] Would recursively copy directory ${copyFile.src} → ${copyFile.dest}`));
71
73
  } else {
72
74
  const dirSubstitute = !!(copyFile.substitute_variables === true || (
73
75
  copyFile.substitute_variables === undefined &&
@@ -75,24 +77,39 @@ export async function processCopyFiles(
75
77
  template.variables.length > 0 &&
76
78
  Object.keys(variables).length > 0
77
79
  ));
78
- copyDirRecursive(srcPath, destPath, variables, dirSubstitute, copyFile.chmod);
80
+ copyDirRecursive(srcPath, destPath, variables, dirSubstitute, copyFile.chmod, collisionMode, silent);
79
81
  }
80
- console.log(chalk.green(` ✓ ${copyFile.dest} (recursive)`));
82
+ if (!silent) console.log(chalk.green(` ✓ ${copyFile.dest} (recursive)`));
81
83
  } else {
84
+ // Check collision mode
85
+ if (collisionMode === 'newest' && fs.existsSync(destPath)) {
86
+ const destStat = fs.statSync(destPath);
87
+ if (destStat.mtimeMs > stat.mtimeMs) {
88
+ if (dryRun && !silent) {
89
+ console.log(chalk.yellow(` [DRY RUN] [COLLISION] Destination is newer, keeping ${copyFile.dest}`));
90
+ } else if (!silent) {
91
+ console.log(chalk.yellow(` [COLLISION] Destination is newer, keeping ${copyFile.dest}`));
92
+ }
93
+ continue;
94
+ }
95
+ }
96
+
82
97
  // Single file copy
83
98
  if (dryRun) {
84
- console.log(chalk.gray(` [DRY RUN] Would copy ${copyFile.src} → ${copyFile.dest}`));
85
- const drySubstitute = !!(copyFile.substitute_variables === true || (
86
- copyFile.substitute_variables === undefined &&
87
- template.variables &&
88
- template.variables.length > 0 &&
89
- Object.keys(variables).length > 0
90
- ));
91
- if (drySubstitute) {
92
- console.log(chalk.gray(` [DRY RUN] Would substitute variables in ${copyFile.dest}`));
93
- }
94
- if (copyFile.chmod) {
95
- console.log(chalk.gray(` [DRY RUN] Would chmod ${copyFile.chmod} ${copyFile.dest}`));
99
+ if (!silent) {
100
+ console.log(chalk.gray(` [DRY RUN] Would copy ${copyFile.src} ${copyFile.dest}`));
101
+ const drySubstitute = !!(copyFile.substitute_variables === true || (
102
+ copyFile.substitute_variables === undefined &&
103
+ template.variables &&
104
+ template.variables.length > 0 &&
105
+ Object.keys(variables).length > 0
106
+ ));
107
+ if (drySubstitute) {
108
+ console.log(chalk.gray(` [DRY RUN] Would substitute variables in ${copyFile.dest}`));
109
+ }
110
+ if (copyFile.chmod) {
111
+ console.log(chalk.gray(` [DRY RUN] Would chmod ${copyFile.chmod} ${copyFile.dest}`));
112
+ }
96
113
  }
97
114
  continue;
98
115
  }
@@ -119,13 +136,13 @@ export async function processCopyFiles(
119
136
  try {
120
137
  fs.chmodSync(destPath, parseInt(copyFile.chmod, 8));
121
138
  } catch (e) {
122
- if (process.platform !== 'win32') {
139
+ if (process.platform !== 'win32' && !silent) {
123
140
  console.error(chalk.red(`Failed to set chmod ${copyFile.chmod} on ${copyFile.dest}`));
124
141
  }
125
142
  }
126
143
  }
127
144
 
128
- console.log(chalk.green(` ✓ ${copyFile.dest}`));
145
+ if (!silent) console.log(chalk.green(` ✓ ${copyFile.dest}`));
129
146
  }
130
147
  }
131
148
  }
@@ -135,7 +152,9 @@ function copyDirRecursive(
135
152
  dest: string,
136
153
  variables: Record<string, string>,
137
154
  substitute: boolean,
138
- chmod?: string
155
+ chmod?: string,
156
+ collisionMode: 'overwrite' | 'newest' = 'overwrite',
157
+ silent: boolean = false
139
158
  ) {
140
159
  fs.mkdirSync(dest, { recursive: true });
141
160
  const entries = fs.readdirSync(src, { withFileTypes: true });
@@ -145,8 +164,16 @@ function copyDirRecursive(
145
164
  const destPath = path.join(dest, entry.name);
146
165
 
147
166
  if (entry.isDirectory()) {
148
- copyDirRecursive(srcPath, destPath, variables, substitute, chmod);
167
+ copyDirRecursive(srcPath, destPath, variables, substitute, chmod, collisionMode, silent);
149
168
  } else {
169
+ if (collisionMode === 'newest' && fs.existsSync(destPath)) {
170
+ const destStat = fs.statSync(destPath);
171
+ const srcStat = fs.statSync(srcPath);
172
+ if (destStat.mtimeMs > srcStat.mtimeMs) {
173
+ continue;
174
+ }
175
+ }
176
+
150
177
  let content = fs.readFileSync(srcPath, 'utf-8');
151
178
  if (substitute) {
152
179
  content = substituteVariables(content, variables);