Skip to content

Commit

Permalink
feat: publish package migrations
Browse files Browse the repository at this point in the history
  • Loading branch information
kiddyuchina committed Apr 2, 2024
1 parent a6b7df8 commit 3f70a4a
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
29 changes: 29 additions & 0 deletions bin/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,35 @@ program
}
});

program
.command('migrate:publish <package>')
.description('Publish any migration files from packages.')
.action(async (package, opts) => {
if (!env.configPath) {
exit('Error: sutando config not found. Run `sutando init` first.');
}

const config = require(env.configPath);

try {
const packagePath = findModulePkg(package);

if (!packagePath) {
exit(`Error: package ${package} not found`);
}

const MigrationCreator = getSutandoModule('src/migrations/migration-creator');
const creator = new MigrationCreator(path.join(packagePath, 'migrations'));

console.log(color.green(`Publishing migrations:`));
const fileNames = await creator.publish(env.cwd + `/${config?.migrations?.path || 'migrations'}`, (fileName, oldPath, newPath) => {
console.log(newPath + ' ' + color.green(`DONE`));
});
} catch (err) {
exit(err);
}
});

program
.command('migrate:run')
.description('Run all pending migrations.')
Expand Down
13 changes: 13 additions & 0 deletions src/migrations/migration-creator.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,19 @@ class MigrationCreator {
return filePath;
}

async publish(dir, callback) {
const migrationFiles = await promisify(fs.readdir)(this.customStubPath);
await this.ensureDirectoryExists(dir);

for (const migrationFile of migrationFiles) {
const sourceFilePath = path.join(this.customStubPath, migrationFile);
const destinationFilePath = path.join(dir, migrationFile);

await promisify(fs.copyFile)(sourceFilePath, destinationFilePath);
callback && await callback(migrationFile, sourceFilePath, destinationFilePath);
}
}

async ensureMigrationDoesntAlreadyExist(name, dir) {
const migrationFiles = await promisify(fs.glob)(path.join(dir, '*.js'));

Expand Down

0 comments on commit 3f70a4a

Please sign in to comment.