Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ program
.option('--host <host>', 'Management API host')
.option('--config <path/to/config>', 'Config file path (disables auto detect)')
.option('--cwd <directory>', 'Working directory. Defaults to process.cwd()')
.option('-n, --name <name>', 'Set a name for the migration')
.description('Generate a new Contentful migration')
.action(
actionRunner(async (cmd) => {
Expand All @@ -164,7 +165,7 @@ program
'environmentId',
'directory',
]);
await createMigration(config);
await createMigration({ ...config, name: cmd.name });
})
);

Expand Down
11 changes: 11 additions & 0 deletions lib/helpers/slugify.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
const slugify = (text) =>
text
.toString()
.replace(/([a-z])([A-Z])/g, '$1-$2')

Check warning on line 4 in lib/helpers/slugify.js

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Prefer `String#replaceAll()` over `String#replace()`.

See more on https://sonarcloud.io/project/issues?id=jungvonmatt_contentful-migrations&issues=AZ0BBWep9xZfPpEomC1d&open=AZ0BBWep9xZfPpEomC1d&pullRequest=85
.toLowerCase()
.trim()
.replace(/[^a-z0-9]+/g, '-')

Check warning on line 7 in lib/helpers/slugify.js

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Prefer `String#replaceAll()` over `String#replace()`.

See more on https://sonarcloud.io/project/issues?id=jungvonmatt_contentful-migrations&issues=AZ0ApoL9UsnleY9HJiOe&open=AZ0ApoL9UsnleY9HJiOe&pullRequest=85
.replace(/^-/, '')
.replace(/-$/, '');
Comment on lines +1 to +9

module.exports = { slugify };
54 changes: 54 additions & 0 deletions lib/helpers/slugify.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
const { slugify } = require('./slugify');

describe('slugify', () => {
it('converts spaces to hyphens and lowercases', () => {
expect(slugify('Add this new feature')).toBe('add-this-new-feature');
});

it('removes special characters', () => {
expect(slugify('Hello, World!')).toBe('hello-world');
});

it('trims leading and trailing whitespace', () => {
expect(slugify(' spaced out ')).toBe('spaced-out');
});

it('strips leading and trailing hyphens', () => {
expect(slugify('--hello--')).toBe('hello');
});

it('collapses consecutive special characters into a single hyphen', () => {
expect(slugify('foo---bar baz')).toBe('foo-bar-baz');
});

it('returns empty string for whitespace-only input', () => {
expect(slugify(' ')).toBe('');
});

it('returns empty string for invalid string input', () => {
expect(slugify('')).toBe('');
expect(slugify('-')).toBe('');
expect(slugify('%/&((§/')).toBe('');
});

it('preserves numbers', () => {
expect(slugify('Version 2 update')).toBe('version-2-update');
});

it('handles colons and mixed punctuation', () => {
expect(slugify('Fix: content model!')).toBe('fix-content-model');
});

it('handles leading and trailing hyphens', () => {
expect(slugify('--hello--')).toBe('hello');
});

it('handles underscores', () => {
expect(slugify('_create_user')).toBe('create-user');
});

it('handles camel case', () => {
expect(slugify('pArticle')).toBe('p-article');
expect(slugify('Create pArticle ContentType')).toBe('create-p-article-content-type');
});
});
10 changes: 7 additions & 3 deletions lib/migration.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const { getEnvironment, getOrganizationId } = require('./contentful');
const { confirm, STATE_SUCCESS, STATE_FAILURE } = require('./config');

const { storeMigration, getNewMigrations, getVersionFromFile } = require('./backend');
const { slugify } = require('./helpers/slugify');

const migrationHeader = stripIndent`/* eslint-env node */
const { withHelpers } = require('@jungvonmatt/contentful-migrations');
Expand Down Expand Up @@ -40,11 +41,14 @@ const createMigration = async (config) => {
console.log(err);
}

const { directory } = config || {};
const { directory, name } = config || {};
const timestamp = Date.now();
const filename = path.join(directory, `${timestamp}-migration.${module ? 'cjs' : 'js'}`);
const hasName = name && slugify(name);
const slug = hasName || 'migration';
const filename = path.join(directory, `${timestamp}-${slug}.${module ? 'cjs' : 'js'}`);
const comment = hasName ? `// ${name}` : '// Add your migration code here';
const content = stripIndent`${migrationHeader}
// Add your migration code here
${comment}
})`;
Comment on lines +46 to 52

await fs.outputFile(filename, await format(filename, content, config));
Expand Down
Loading