update to smartconfig

This commit is contained in:
2026-03-24 16:10:51 +00:00
parent eda67395fe
commit d0d922e53b
41 changed files with 425 additions and 2091 deletions

View File

@@ -1,7 +1,6 @@
import { BaseFormatter } from '../classes.baseformatter.js';
import type { IPlannedChange } from '../interfaces.format.js';
import * as plugins from '../mod.plugins.js';
import * as cleanupFormatter from '../format.cleanup.js';
export class CleanupFormatter extends BaseFormatter {
get name(): string {

View File

@@ -17,15 +17,15 @@ export class CopyFormatter extends BaseFormatter {
async analyze(): Promise<IPlannedChange[]> {
const changes: IPlannedChange[] = [];
// Get copy configuration from npmextra.json
const npmextraConfig = new plugins.npmextra.Smartconfig();
const copyConfig = npmextraConfig.dataFor<{ patterns: ICopyPattern[] }>(
// Get copy configuration from .smartconfig.json
const smartconfigInstance = new plugins.smartconfig.Smartconfig();
const copyConfig = smartconfigInstance.dataFor<{ patterns: ICopyPattern[] }>(
'gitzone.format.copy',
{ patterns: [] },
);
if (!copyConfig.patterns || copyConfig.patterns.length === 0) {
logVerbose('No copy patterns configured in npmextra.json');
logVerbose('No copy patterns configured in .smartconfig.json');
return changes;
}
@@ -103,10 +103,6 @@ export class CopyFormatter extends BaseFormatter {
async applyChange(change: IPlannedChange): Promise<void> {
if (!change.content) return;
// Ensure destination directory exists
const destDir = plugins.path.dirname(change.path);
await plugins.smartfs.directory(destDir).recursive().create();
if (change.type === 'create') {
await this.createFile(change.path, change.content);
} else {

View File

@@ -1,42 +1,39 @@
import { BaseFormatter } from '../classes.baseformatter.js';
import type { IPlannedChange } from '../interfaces.format.js';
import * as plugins from '../mod.plugins.js';
import * as paths from '../../paths.js';
import { logger } from '../../gitzone.logging.js';
// Standard gitignore template content (without front-matter)
const GITIGNORE_TEMPLATE = `.nogit/
# artifacts
coverage/
public/
# installs
node_modules/
# caches
.yarn/
.cache/
.rpt2_cache
# builds
dist/
dist_*/
# AI
.claude/
.serena/
#------# custom`;
export class GitignoreFormatter extends BaseFormatter {
get name(): string {
return 'gitignore';
}
/**
* Read the standard gitignore template from the asset file,
* stripping the YAML frontmatter.
*/
private async getStandardTemplate(): Promise<string> {
const templatePath = plugins.path.join(paths.templatesDir, 'gitignore', '_gitignore');
const raw = (await plugins.smartfs
.file(templatePath)
.encoding('utf8')
.read()) as string;
// Strip YAML frontmatter (---\n...\n---)
const frontmatterEnd = raw.indexOf('---', 3);
if (frontmatterEnd !== -1) {
return raw.slice(frontmatterEnd + 3).trimStart();
}
return raw;
}
async analyze(): Promise<IPlannedChange[]> {
const changes: IPlannedChange[] = [];
const gitignorePath = '.gitignore';
const standardTemplate = await this.getStandardTemplate();
// Check if file exists and extract custom content
let customContent = '';
const exists = await plugins.smartfs.file(gitignorePath).exists();
@@ -59,11 +56,11 @@ export class GitignoreFormatter extends BaseFormatter {
}
// Compute new content
let newContent = GITIGNORE_TEMPLATE;
let newContent = standardTemplate;
if (customContent) {
newContent = GITIGNORE_TEMPLATE + '\n' + customContent + '\n';
newContent = standardTemplate + '\n' + customContent + '\n';
} else {
newContent = GITIGNORE_TEMPLATE + '\n';
newContent = standardTemplate + '\n';
}
// Read current content to compare
@@ -75,7 +72,6 @@ export class GitignoreFormatter extends BaseFormatter {
.read()) as string;
}
// Determine change type
if (!exists) {
changes.push({
type: 'create',

View File

@@ -1,174 +0,0 @@
import { BaseFormatter } from '../classes.baseformatter.js';
import type { IPlannedChange } from '../interfaces.format.js';
import * as plugins from '../mod.plugins.js';
import { logger, logVerbose } from '../../gitzone.logging.js';
/**
* Migrates npmextra.json from old namespace keys to new package-scoped keys
*/
const migrateNamespaceKeys = (npmextraJson: any): boolean => {
let migrated = false;
const migrations = [
{ oldKey: 'gitzone', newKey: '@git.zone/cli' },
{ oldKey: 'tsdoc', newKey: '@git.zone/tsdoc' },
{ oldKey: 'npmdocker', newKey: '@git.zone/tsdocker' },
{ oldKey: 'npmci', newKey: '@ship.zone/szci' },
{ oldKey: 'szci', newKey: '@ship.zone/szci' },
];
for (const { oldKey, newKey } of migrations) {
if (npmextraJson[oldKey]) {
if (!npmextraJson[newKey]) {
// New key doesn't exist - simple rename
npmextraJson[newKey] = npmextraJson[oldKey];
} else {
// New key exists - merge old into new (old values don't overwrite new)
npmextraJson[newKey] = {
...npmextraJson[oldKey],
...npmextraJson[newKey],
};
}
delete npmextraJson[oldKey];
migrated = true;
}
}
return migrated;
};
/**
* Migrates npmAccessLevel from @ship.zone/szci to @git.zone/cli.release.accessLevel
*/
const migrateAccessLevel = (npmextraJson: any): boolean => {
const szciConfig = npmextraJson['@ship.zone/szci'];
if (!szciConfig?.npmAccessLevel) {
return false;
}
const gitzoneConfig = npmextraJson['@git.zone/cli'] || {};
if (gitzoneConfig?.release?.accessLevel) {
delete szciConfig.npmAccessLevel;
return true;
}
if (!npmextraJson['@git.zone/cli']) {
npmextraJson['@git.zone/cli'] = {};
}
if (!npmextraJson['@git.zone/cli'].release) {
npmextraJson['@git.zone/cli'].release = {};
}
npmextraJson['@git.zone/cli'].release.accessLevel = szciConfig.npmAccessLevel;
delete szciConfig.npmAccessLevel;
return true;
};
export class NpmextraFormatter extends BaseFormatter {
get name(): string {
return 'npmextra';
}
async analyze(): Promise<IPlannedChange[]> {
const changes: IPlannedChange[] = [];
const npmextraPath = 'smartconfig.json';
// Check if file exists
const exists = await plugins.smartfs.file(npmextraPath).exists();
if (!exists) {
logVerbose('npmextra.json does not exist, skipping');
return changes;
}
// Read current content
const currentContent = (await plugins.smartfs
.file(npmextraPath)
.encoding('utf8')
.read()) as string;
// Parse and compute new content
const npmextraJson = JSON.parse(currentContent);
// Apply migrations (these are automatic, non-interactive)
migrateNamespaceKeys(npmextraJson);
migrateAccessLevel(npmextraJson);
// Ensure namespaces exist
if (!npmextraJson['@git.zone/cli']) {
npmextraJson['@git.zone/cli'] = {};
}
if (!npmextraJson['@ship.zone/szci']) {
npmextraJson['@ship.zone/szci'] = {};
}
const newContent = JSON.stringify(npmextraJson, null, 2);
// Only add change if content differs
if (newContent !== currentContent) {
changes.push({
type: 'modify',
path: npmextraPath,
module: this.name,
description: 'Migrate and format npmextra.json',
content: newContent,
});
}
return changes;
}
async applyChange(change: IPlannedChange): Promise<void> {
if (change.type !== 'modify' || !change.content) return;
// Parse the content to check for missing required fields
const npmextraJson = JSON.parse(change.content);
// Check for missing required module information
const expectedRepoInformation: string[] = [
'projectType',
'module.githost',
'module.gitscope',
'module.gitrepo',
'module.description',
'module.npmPackagename',
'module.license',
];
const interactInstance = new plugins.smartinteract.SmartInteract();
for (const expectedRepoInformationItem of expectedRepoInformation) {
if (
!plugins.smartobject.smartGet(
npmextraJson['@git.zone/cli'],
expectedRepoInformationItem,
)
) {
interactInstance.addQuestions([
{
message: `What is the value of ${expectedRepoInformationItem}`,
name: expectedRepoInformationItem,
type: 'input',
default: 'undefined variable',
},
]);
}
}
const answerbucket = await interactInstance.runQueue();
for (const expectedRepoInformationItem of expectedRepoInformation) {
const cliProvidedValue = answerbucket.getAnswerFor(
expectedRepoInformationItem,
);
if (cliProvidedValue) {
plugins.smartobject.smartAdd(
npmextraJson['@git.zone/cli'],
expectedRepoInformationItem,
cliProvidedValue,
);
}
}
// Write the final content
const finalContent = JSON.stringify(npmextraJson, null, 2);
await this.modifyFile(change.path, finalContent);
logger.log('info', 'Updated npmextra.json');
}
}

View File

@@ -100,9 +100,9 @@ export class PackageJsonFormatter extends BaseFormatter {
// Parse and compute new content
const packageJson = JSON.parse(currentContent);
// Get gitzone config from npmextra
const npmextraConfig = new plugins.npmextra.Smartconfig(paths.cwd);
const gitzoneData: any = npmextraConfig.dataFor('@git.zone/cli', {});
// Get gitzone config from smartconfig
const smartconfigInstance = new plugins.smartconfig.Smartconfig(paths.cwd);
const gitzoneData: any = smartconfigInstance.dataFor('@git.zone/cli', {});
// Set metadata from gitzone config
if (gitzoneData.module) {
@@ -156,7 +156,7 @@ export class PackageJsonFormatter extends BaseFormatter {
'dist_ts_web/**/*',
'assets/**/*',
'cli.js',
'smartconfig.json',
'.smartconfig.json',
'readme.md',
];

View File

@@ -21,7 +21,7 @@ export class PrettierFormatter extends BaseFormatter {
const rootConfigFiles = [
'package.json',
'tsconfig.json',
'smartconfig.json',
'.smartconfig.json',
'.prettierrc',
'.prettierrc.json',
'.prettierrc.js',
@@ -79,12 +79,9 @@ export class PrettierFormatter extends BaseFormatter {
// Remove duplicates
const uniqueFiles = [...new Set(allFiles)];
// Get all files that match the pattern
const files = uniqueFiles;
// Ensure we only process actual files (not directories)
const validFiles: string[] = [];
for (const file of files) {
for (const file of uniqueFiles) {
try {
const stats = await plugins.smartfs.file(file).stat();
if (!stats.isDirectory) {
@@ -96,14 +93,7 @@ export class PrettierFormatter extends BaseFormatter {
}
}
// Check which files need formatting
for (const file of validFiles) {
// Skip files that haven't changed
if (!(await this.shouldProcessFile(file))) {
logVerbose(`Skipping ${file} - no changes detected`);
continue;
}
changes.push({
type: 'modify',
path: file,
@@ -232,7 +222,7 @@ export class PrettierFormatter extends BaseFormatter {
private async getPrettierConfig(): Promise<any> {
// Try to load prettier config from the project
const prettierConfig = new plugins.npmextra.Smartconfig();
const prettierConfig = new plugins.smartconfig.Smartconfig();
return prettierConfig.dataFor('prettier', {
// Default prettier config
singleQuote: true,

View File

@@ -0,0 +1,213 @@
import { BaseFormatter } from '../classes.baseformatter.js';
import type { IPlannedChange } from '../interfaces.format.js';
import * as plugins from '../mod.plugins.js';
import { logger, logVerbose } from '../../gitzone.logging.js';
/**
* Migrates .smartconfig.json from old namespace keys to new package-scoped keys
*/
const migrateNamespaceKeys = (smartconfigJson: any): boolean => {
let migrated = false;
const migrations = [
{ oldKey: 'gitzone', newKey: '@git.zone/cli' },
{ oldKey: 'tsdoc', newKey: '@git.zone/tsdoc' },
{ oldKey: 'npmdocker', newKey: '@git.zone/tsdocker' },
{ oldKey: 'npmci', newKey: '@ship.zone/szci' },
{ oldKey: 'szci', newKey: '@ship.zone/szci' },
];
for (const { oldKey, newKey } of migrations) {
if (smartconfigJson[oldKey]) {
if (!smartconfigJson[newKey]) {
smartconfigJson[newKey] = smartconfigJson[oldKey];
} else {
smartconfigJson[newKey] = {
...smartconfigJson[oldKey],
...smartconfigJson[newKey],
};
}
delete smartconfigJson[oldKey];
migrated = true;
}
}
return migrated;
};
/**
* Migrates npmAccessLevel from @ship.zone/szci to @git.zone/cli.release.accessLevel
*/
const migrateAccessLevel = (smartconfigJson: any): boolean => {
const szciConfig = smartconfigJson['@ship.zone/szci'];
if (!szciConfig?.npmAccessLevel) {
return false;
}
const gitzoneConfig = smartconfigJson['@git.zone/cli'] || {};
if (gitzoneConfig?.release?.accessLevel) {
delete szciConfig.npmAccessLevel;
return true;
}
if (!smartconfigJson['@git.zone/cli']) {
smartconfigJson['@git.zone/cli'] = {};
}
if (!smartconfigJson['@git.zone/cli'].release) {
smartconfigJson['@git.zone/cli'].release = {};
}
smartconfigJson['@git.zone/cli'].release.accessLevel = szciConfig.npmAccessLevel;
delete szciConfig.npmAccessLevel;
return true;
};
// Config file names in priority order (newest → oldest)
const CONFIG_FILE_NAMES = ['.smartconfig.json', 'smartconfig.json', 'npmextra.json'];
const TARGET_CONFIG_FILE = '.smartconfig.json';
export class SmartconfigFormatter extends BaseFormatter {
get name(): string {
return 'smartconfig';
}
/**
* Find the config file, checking in priority order.
* Returns the path and whether it needs renaming.
*/
private async findConfigFile(): Promise<{ path: string; needsRename: boolean } | null> {
for (const filename of CONFIG_FILE_NAMES) {
const exists = await plugins.smartfs.file(filename).exists();
if (exists) {
return {
path: filename,
needsRename: filename !== TARGET_CONFIG_FILE,
};
}
}
return null;
}
async analyze(): Promise<IPlannedChange[]> {
const changes: IPlannedChange[] = [];
const configFile = await this.findConfigFile();
if (!configFile) {
logVerbose('No config file found (.smartconfig.json, smartconfig.json, or npmextra.json), skipping');
return changes;
}
// Read current content
const currentContent = (await plugins.smartfs
.file(configFile.path)
.encoding('utf8')
.read()) as string;
// Parse and apply migrations
const smartconfigJson = JSON.parse(currentContent);
migrateNamespaceKeys(smartconfigJson);
migrateAccessLevel(smartconfigJson);
// Ensure namespaces exist
if (!smartconfigJson['@git.zone/cli']) {
smartconfigJson['@git.zone/cli'] = {};
}
if (!smartconfigJson['@ship.zone/szci']) {
smartconfigJson['@ship.zone/szci'] = {};
}
const newContent = JSON.stringify(smartconfigJson, null, 2);
// If file needs renaming, plan a create + delete
if (configFile.needsRename) {
changes.push({
type: 'create',
path: TARGET_CONFIG_FILE,
module: this.name,
description: `Migrate ${configFile.path} to ${TARGET_CONFIG_FILE}`,
content: newContent,
});
changes.push({
type: 'delete',
path: configFile.path,
module: this.name,
description: `Remove old ${configFile.path}`,
});
} else if (newContent !== currentContent) {
// File is already .smartconfig.json, just needs content update
changes.push({
type: 'modify',
path: TARGET_CONFIG_FILE,
module: this.name,
description: 'Migrate and format .smartconfig.json',
content: newContent,
});
}
return changes;
}
async applyChange(change: IPlannedChange): Promise<void> {
if (change.type === 'delete') {
await this.deleteFile(change.path);
logger.log('info', `Removed old config file ${change.path}`);
return;
}
if (!change.content) return;
// Parse the content to check for missing required fields
const smartconfigJson = JSON.parse(change.content);
const expectedRepoInformation: string[] = [
'projectType',
'module.githost',
'module.gitscope',
'module.gitrepo',
'module.description',
'module.npmPackagename',
'module.license',
];
const interactInstance = new plugins.smartinteract.SmartInteract();
for (const expectedRepoInformationItem of expectedRepoInformation) {
if (
!plugins.smartobject.smartGet(
smartconfigJson['@git.zone/cli'],
expectedRepoInformationItem,
)
) {
interactInstance.addQuestions([
{
message: `What is the value of ${expectedRepoInformationItem}`,
name: expectedRepoInformationItem,
type: 'input',
default: 'undefined variable',
},
]);
}
}
const answerbucket = await interactInstance.runQueue();
for (const expectedRepoInformationItem of expectedRepoInformation) {
const cliProvidedValue = answerbucket.getAnswerFor(
expectedRepoInformationItem,
);
if (cliProvidedValue) {
plugins.smartobject.smartAdd(
smartconfigJson['@git.zone/cli'],
expectedRepoInformationItem,
cliProvidedValue,
);
}
}
const finalContent = JSON.stringify(smartconfigJson, null, 2);
if (change.type === 'create') {
await this.createFile(change.path, finalContent);
} else {
await this.modifyFile(change.path, finalContent);
}
logger.log('info', `Updated ${change.path}`);
}
}

View File

@@ -62,9 +62,6 @@ export class TemplatesFormatter extends BaseFormatter {
{ templatePath: 'html/index.html', destPath: 'html/index.html' },
]);
changes.push(...websiteChanges);
} else if (projectType === 'service') {
const serviceChanges = await this.analyzeTemplate('service_update', []);
changes.push(...serviceChanges);
} else if (projectType === 'wcc') {
const wccChanges = await this.analyzeTemplate('wcc_update', [
{ templatePath: 'html/index.html', destPath: 'html/index.html' },
@@ -139,12 +136,6 @@ export class TemplatesFormatter extends BaseFormatter {
async applyChange(change: IPlannedChange): Promise<void> {
if (!change.content) return;
// Ensure destination directory exists
const destDir = plugins.path.dirname(change.path);
if (destDir && destDir !== '.') {
await plugins.smartfs.directory(destDir).recursive().create();
}
if (change.type === 'create') {
await this.createFile(change.path, change.content);
} else {

View File

@@ -30,9 +30,10 @@ export class TsconfigFormatter extends BaseFormatter {
const tsconfigObject = JSON.parse(currentContent);
tsconfigObject.compilerOptions = tsconfigObject.compilerOptions || {};
tsconfigObject.compilerOptions.baseUrl = '.';
tsconfigObject.compilerOptions.paths = {};
const existingPaths = tsconfigObject.compilerOptions.paths || {};
// Get module paths from tspublish
// Get module paths from tspublish, merging with existing custom paths
const tspublishPaths: Record<string, string[]> = {};
try {
const tsPublishMod = await import('@git.zone/tspublish');
const tsPublishInstance = new tsPublishMod.TsPublish();
@@ -40,7 +41,7 @@ export class TsconfigFormatter extends BaseFormatter {
for (const publishModule of Object.keys(publishModules)) {
const publishConfig = publishModules[publishModule];
tsconfigObject.compilerOptions.paths[`${publishConfig.name}`] = [
tspublishPaths[`${publishConfig.name}`] = [
`./${publishModule}/index.js`,
];
}
@@ -48,6 +49,8 @@ export class TsconfigFormatter extends BaseFormatter {
logVerbose(`Could not get tspublish modules: ${error.message}`);
}
tsconfigObject.compilerOptions.paths = { ...existingPaths, ...tspublishPaths };
const newContent = JSON.stringify(tsconfigObject, null, 2);
// Only add change if content differs