mirror of
https://github.com/laurent22/joplin.git
synced 2025-01-02 12:47:41 +02:00
Generator: Fixed package name conversion and added test units
This commit is contained in:
parent
ab6f02a949
commit
d145ce1876
@ -3,53 +3,7 @@
|
|||||||
const Generator = require('yeoman-generator');
|
const Generator = require('yeoman-generator');
|
||||||
const chalk = require('chalk');
|
const chalk = require('chalk');
|
||||||
const yosay = require('yosay');
|
const yosay = require('yosay');
|
||||||
const slugify = require('slugify');
|
const { mergePackageKey, mergeIgnoreFile, packageNameFromPluginName } = require('./utils');
|
||||||
|
|
||||||
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);
|
|
||||||
}
|
|
||||||
|
|
||||||
module.exports = class extends Generator {
|
module.exports = class extends Generator {
|
||||||
|
|
||||||
|
68
packages/generator-joplin/generators/app/utils.js
Normal file
68
packages/generator-joplin/generators/app/utils.js
Normal file
@ -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 };
|
32
packages/generator-joplin/generators/app/utils.test.js
Normal file
32
packages/generator-joplin/generators/app/utils.test.js
Normal file
@ -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);
|
||||||
|
});
|
||||||
|
|
||||||
|
});
|
11
packages/generator-joplin/jest.config.js
Normal file
11
packages/generator-joplin/jest.config.js
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
module.exports = {
|
||||||
|
testMatch: [
|
||||||
|
'**/*.test.js',
|
||||||
|
],
|
||||||
|
|
||||||
|
testPathIgnorePatterns: [
|
||||||
|
'<rootDir>/node_modules/',
|
||||||
|
],
|
||||||
|
|
||||||
|
testEnvironment: 'node',
|
||||||
|
};
|
4315
packages/generator-joplin/package-lock.json
generated
4315
packages/generator-joplin/package-lock.json
generated
File diff suppressed because it is too large
Load Diff
@ -7,6 +7,10 @@
|
|||||||
"name": "Laurent Cozic",
|
"name": "Laurent Cozic",
|
||||||
"url": "https://github.com/laurent22/joplin/tree/dev/packages/generator-joplin"
|
"url": "https://github.com/laurent22/joplin/tree/dev/packages/generator-joplin"
|
||||||
},
|
},
|
||||||
|
"scripts": {
|
||||||
|
"test": "jest",
|
||||||
|
"test-ci": "npm run test"
|
||||||
|
},
|
||||||
"files": [
|
"files": [
|
||||||
"generators"
|
"generators"
|
||||||
],
|
],
|
||||||
@ -24,7 +28,10 @@
|
|||||||
"yeoman-generator": "^2.0.1",
|
"yeoman-generator": "^2.0.1",
|
||||||
"yosay": "^2.0.1"
|
"yosay": "^2.0.1"
|
||||||
},
|
},
|
||||||
|
"devDependencies": {
|
||||||
|
"jest": "^26.6.3"
|
||||||
|
},
|
||||||
"repository": "https://github.com/laurent22/generator-joplin",
|
"repository": "https://github.com/laurent22/generator-joplin",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"private": true
|
"private": true
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user