mirror of https://github.com/laurent22/joplin.git
Generator: Fixed package name conversion and added test units
parent
ab6f02a949
commit
d145ce1876
|
@ -3,53 +3,7 @@
|
|||
const Generator = require('yeoman-generator');
|
||||
const chalk = require('chalk');
|
||||
const yosay = require('yosay');
|
||||
const slugify = require('slugify');
|
||||
|
||||
function mergePackageKey(parentKey, source, dest) {
|
||||
const output = Object.assign({}, dest);
|
||||
|
||||
for (const k in source) {
|
||||
if (k === 'keywords' && !Array.isArray(output[k])) {
|
||||
// Fix an earlier bugs where keywords were set to an empty object
|
||||
output[k] = source[k];
|
||||
} else if (k === 'keywords') {
|
||||
// For keywords, make sure to add the "joplin-plugin" one
|
||||
if (!output['keywords']) output['keywords'] = [];
|
||||
if (output['keywords'].indexOf('joplin-plugin') < 0) output['keywords'].push('joplin-plugin');
|
||||
} else if (!(k in output)) {
|
||||
// If the key doesn't exist in the destination, add it
|
||||
output[k] = source[k];
|
||||
} else if (parentKey === 'devDependencies') {
|
||||
// If we are dealing with the dependencies, overwrite with the
|
||||
// version from source.
|
||||
output[k] = source[k];
|
||||
} else if (typeof source[k] === 'object' && !Array.isArray(source[k]) && source[k] !== null) {
|
||||
// If it's an object, recursively process it
|
||||
output[k] = mergePackageKey(k, source[k], output[k]);
|
||||
} else {
|
||||
// Otherwise, the default is to preserve the destination key
|
||||
output[k] = dest[k];
|
||||
}
|
||||
}
|
||||
|
||||
return output;
|
||||
}
|
||||
|
||||
function mergeIgnoreFile(source, dest) {
|
||||
const output = source.split('\n').concat(dest.split('\n'));
|
||||
|
||||
return output.filter(function(item, pos) {
|
||||
if (!item) return true; // Leave blank lines
|
||||
return output.indexOf(item) === pos;
|
||||
}).join('\n');
|
||||
}
|
||||
|
||||
function packageNameFromPluginName(pluginName) {
|
||||
// Package name is limited to 214 characters
|
||||
return `joplin-plugin-${slugify(pluginName, {
|
||||
lower: true,
|
||||
})}`.substr(0, 214);
|
||||
}
|
||||
const { mergePackageKey, mergeIgnoreFile, packageNameFromPluginName } = require('./utils');
|
||||
|
||||
module.exports = class extends Generator {
|
||||
|
||||
|
|
|
@ -0,0 +1,68 @@
|
|||
const slugify = require('slugify');
|
||||
|
||||
function mergePackageKey(parentKey, source, dest) {
|
||||
const output = Object.assign({}, dest);
|
||||
|
||||
for (const k in source) {
|
||||
if (k === 'keywords' && !Array.isArray(output[k])) {
|
||||
// Fix an earlier bugs where keywords were set to an empty object
|
||||
output[k] = source[k];
|
||||
} else if (k === 'keywords') {
|
||||
// For keywords, make sure to add the "joplin-plugin" one
|
||||
if (!output['keywords']) output['keywords'] = [];
|
||||
if (output['keywords'].indexOf('joplin-plugin') < 0) output['keywords'].push('joplin-plugin');
|
||||
} else if (!(k in output)) {
|
||||
// If the key doesn't exist in the destination, add it
|
||||
output[k] = source[k];
|
||||
} else if (parentKey === 'devDependencies') {
|
||||
// If we are dealing with the dependencies, overwrite with the
|
||||
// version from source.
|
||||
output[k] = source[k];
|
||||
} else if (typeof source[k] === 'object' && !Array.isArray(source[k]) && source[k] !== null) {
|
||||
// If it's an object, recursively process it
|
||||
output[k] = mergePackageKey(k, source[k], output[k]);
|
||||
} else {
|
||||
// Otherwise, the default is to preserve the destination key
|
||||
output[k] = dest[k];
|
||||
}
|
||||
}
|
||||
|
||||
return output;
|
||||
}
|
||||
|
||||
function mergeIgnoreFile(source, dest) {
|
||||
const output = source.split('\n').concat(dest.split('\n'));
|
||||
|
||||
return output.filter(function(item, pos) {
|
||||
if (!item) return true; // Leave blank lines
|
||||
return output.indexOf(item) === pos;
|
||||
}).join('\n');
|
||||
}
|
||||
|
||||
function packageNameFromPluginName(pluginName) {
|
||||
let output = pluginName;
|
||||
|
||||
// Replace all special characters with '-'
|
||||
output = output.replace(/[*+~.()'"!:@[\]]/g, '-');
|
||||
|
||||
// Slugify to replace non-alphabetical characters by letters
|
||||
output = slugify(output, { lower: true });
|
||||
|
||||
// Trim any remaining "-" from beginning of string
|
||||
output = output.replace(/^[-]+/, '');
|
||||
|
||||
if (!output) throw new Error(`This plugin name cannot be converted to a package name: ${pluginName}`);
|
||||
|
||||
// Add prefix
|
||||
output = `joplin-plugin-${output}`;
|
||||
|
||||
// Package name is limited to 214 characters
|
||||
output = output.substr(0, 214);
|
||||
|
||||
// Trim any remaining "-" from end of string
|
||||
output = output.replace(/[-]+$/, '');
|
||||
|
||||
return output;
|
||||
}
|
||||
|
||||
module.exports = { mergePackageKey, mergeIgnoreFile, packageNameFromPluginName };
|
|
@ -0,0 +1,32 @@
|
|||
const { packageNameFromPluginName } = require('./utils');
|
||||
|
||||
describe('utils', () => {
|
||||
|
||||
test('packageNameFromPluginName', () => {
|
||||
const testCases = [
|
||||
['That\'s my plugin!', 'joplin-plugin-that-s-my-plugin'],
|
||||
['with-dashes', 'joplin-plugin-with-dashes'],
|
||||
['with.dots...', 'joplin-plugin-with-dots'],
|
||||
['¡¡¡front dashes!!!', 'joplin-plugin-front-dashes'],
|
||||
['That [will] [be] removed', 'joplin-plugin-that-will-be-removed'],
|
||||
['very very very long name very very very long name very very very long name very very very long name very very very long name very very very long name very very very long name very very very long name very very very long name', 'joplin-plugin-very-very-very-long-name-very-very-very-long-name-very-very-very-long-name-very-very-very-long-name-very-very-very-long-name-very-very-very-long-name-very-very-very-long-name-very-very-very-long-name'],
|
||||
];
|
||||
|
||||
for (const t of testCases) {
|
||||
const input = t[0];
|
||||
const expected = t[1];
|
||||
const actual = packageNameFromPluginName(input);
|
||||
expect(actual).toBe(expected);
|
||||
}
|
||||
|
||||
let hasThrown = false;
|
||||
try {
|
||||
packageNameFromPluginName('');
|
||||
} catch (error) {
|
||||
hasThrown = true;
|
||||
}
|
||||
|
||||
expect(hasThrown).toBe(true);
|
||||
});
|
||||
|
||||
});
|
|
@ -0,0 +1,11 @@
|
|||
module.exports = {
|
||||
testMatch: [
|
||||
'**/*.test.js',
|
||||
],
|
||||
|
||||
testPathIgnorePatterns: [
|
||||
'<rootDir>/node_modules/',
|
||||
],
|
||||
|
||||
testEnvironment: 'node',
|
||||
};
|
File diff suppressed because it is too large
Load Diff
|
@ -7,6 +7,10 @@
|
|||
"name": "Laurent Cozic",
|
||||
"url": "https://github.com/laurent22/joplin/tree/dev/packages/generator-joplin"
|
||||
},
|
||||
"scripts": {
|
||||
"test": "jest",
|
||||
"test-ci": "npm run test"
|
||||
},
|
||||
"files": [
|
||||
"generators"
|
||||
],
|
||||
|
@ -24,6 +28,9 @@
|
|||
"yeoman-generator": "^2.0.1",
|
||||
"yosay": "^2.0.1"
|
||||
},
|
||||
"devDependencies": {
|
||||
"jest": "^26.6.3"
|
||||
},
|
||||
"repository": "https://github.com/laurent22/generator-joplin",
|
||||
"license": "MIT",
|
||||
"private": true
|
||||
|
|
Loading…
Reference in New Issue