2021-01-08 02:52:45 +00:00
'use strict' ;
2020-11-02 16:13:24 +00:00
/ *
2021-01-10 23:35:50 +00:00
* Usage -- see README . md
*
* Normal usage : node bin / plugins / checkPlugin . js ep _whatever
* Auto fix the things it can : node bin / plugins / checkPlugin . js ep _whatever autofix
* Auto commit , push and publish to npm ( highly dangerous ) :
* node bin / plugins / checkPlugin . js ep _whatever autocommit
* /
2020-11-02 16:13:24 +00:00
2021-01-18 08:53:15 +00:00
// As of v14, Node.js does not exit when there is an unhandled Promise rejection. Convert an
// unhandled rejection into an uncaught exception, which does cause Node.js to exit.
process . on ( 'unhandledRejection' , ( err ) => { throw err ; } ) ;
2020-11-23 18:21:51 +00:00
const fs = require ( 'fs' ) ;
2021-01-08 02:52:45 +00:00
const childProcess = require ( 'child_process' ) ;
2020-11-02 16:13:24 +00:00
// get plugin name & path from user input
const pluginName = process . argv [ 2 ] ;
2021-01-18 08:53:15 +00:00
if ( ! pluginName ) throw new Error ( 'no plugin name specified' ) ;
2020-11-02 16:13:24 +00:00
2020-11-23 18:21:51 +00:00
const pluginPath = ` node_modules/ ${ pluginName } ` ;
2020-11-02 16:13:24 +00:00
2020-11-23 18:21:51 +00:00
console . log ( ` Checking the plugin: ${ pluginName } ` ) ;
2020-11-02 16:13:24 +00:00
2021-01-08 23:07:55 +00:00
const optArgs = process . argv . slice ( 3 ) ;
2021-01-08 02:52:45 +00:00
const autoCommit = optArgs . indexOf ( 'autocommit' ) !== - 1 ;
2021-01-11 00:22:59 +00:00
const autoFix = autoCommit || optArgs . indexOf ( 'autofix' ) !== - 1 ;
2020-11-02 16:13:24 +00:00
2021-01-09 04:04:11 +00:00
const execSync = ( cmd , opts = { } ) => ( childProcess . execSync ( cmd , {
cwd : ` ${ pluginPath } / ` ,
... opts ,
} ) || '' ) . toString ( ) . replace ( /\n+$/ , '' ) ;
2021-01-09 23:28:11 +00:00
const writePackageJson = ( obj ) => {
let s = JSON . stringify ( obj , null , 2 ) ;
if ( s . length && s . slice ( s . length - 1 ) !== '\n' ) s += '\n' ;
return fs . writeFileSync ( ` ${ pluginPath } /package.json ` , s ) ;
} ;
2021-01-09 23:27:37 +00:00
2021-01-09 23:51:31 +00:00
const updateDeps = ( parsedPackageJson , key , wantDeps ) => {
const { [ key ] : deps = { } } = parsedPackageJson ;
let changed = false ;
2021-01-10 21:23:04 +00:00
for ( const [ pkg , verInfo ] of Object . entries ( wantDeps ) ) {
const { ver , overwrite = true } = typeof verInfo === 'string' ? { ver : verInfo } : verInfo ;
2021-01-09 23:51:31 +00:00
if ( deps [ pkg ] === ver ) continue ;
if ( deps [ pkg ] == null ) {
console . warn ( ` Missing dependency in ${ key } : ' ${ pkg } ': ' ${ ver } ' ` ) ;
} else {
2021-01-10 21:23:04 +00:00
if ( ! overwrite ) continue ;
2021-01-09 23:51:31 +00:00
console . warn ( ` Dependency mismatch in ${ key } : ' ${ pkg } ': ' ${ ver } ' (current: ${ deps [ pkg ] } ) ` ) ;
}
if ( autoFix ) {
deps [ pkg ] = ver ;
changed = true ;
}
}
if ( changed ) {
parsedPackageJson [ key ] = deps ;
writePackageJson ( parsedPackageJson ) ;
}
} ;
2021-01-09 05:08:43 +00:00
const prepareRepo = ( ) => {
let branch = execSync ( 'git symbolic-ref HEAD' ) ;
if ( branch !== 'refs/heads/master' && branch !== 'refs/heads/main' ) {
throw new Error ( 'master/main must be checked out' ) ;
}
branch = branch . replace ( /^refs\/heads\// , '' ) ;
execSync ( 'git rev-parse --verify -q HEAD^0 || ' +
` { echo "Error: no commits on ${ branch } " >&2; exit 1; } ` ) ;
execSync ( 'git rev-parse --verify @{u}' ) ; // Make sure there's a remote tracking branch.
2021-01-09 22:30:41 +00:00
const modified = execSync ( 'git diff-files --name-status' ) ;
if ( modified !== '' ) throw new Error ( ` working directory has modifications: \n ${ modified } ` ) ;
const untracked = execSync ( 'git ls-files -o --exclude-standard' ) ;
if ( untracked !== '' ) throw new Error ( ` working directory has untracked files: \n ${ untracked } ` ) ;
2021-01-09 22:27:51 +00:00
const indexStatus = execSync ( 'git diff-index --cached --name-status HEAD' ) ;
2021-01-09 05:08:43 +00:00
if ( indexStatus !== '' ) throw new Error ( ` uncommitted staged changes to files: \n ${ indexStatus } ` ) ;
execSync ( 'git pull --ff-only' , { stdio : 'inherit' } ) ;
if ( execSync ( 'git rev-list @{u}...' ) !== '' ) throw new Error ( 'repo contains unpushed commits' ) ;
if ( autoCommit ) {
execSync ( 'git config --get user.name' ) ;
execSync ( 'git config --get user.email' ) ;
}
} ;
2020-11-23 18:21:51 +00:00
if ( autoCommit ) {
console . warn ( 'Auto commit is enabled, I hope you know what you are doing...' ) ;
2020-11-02 16:13:24 +00:00
}
2020-11-23 18:21:51 +00:00
fs . readdir ( pluginPath , ( err , rootFiles ) => {
// handling error
2020-11-02 16:13:24 +00:00
if ( err ) {
2020-11-23 18:21:51 +00:00
return console . log ( ` Unable to scan directory: ${ err } ` ) ;
2020-11-02 16:13:24 +00:00
}
// rewriting files to lower case
2020-11-23 18:21:51 +00:00
const files = [ ] ;
2020-11-02 16:13:24 +00:00
// some files we need to know the actual file name. Not compulsory but might help in the future.
2020-11-23 18:21:51 +00:00
let readMeFileName ;
let repository ;
2020-11-02 16:13:24 +00:00
2020-11-23 18:21:51 +00:00
for ( let i = 0 ; i < rootFiles . length ; i ++ ) {
if ( rootFiles [ i ] . toLowerCase ( ) . indexOf ( 'readme' ) !== - 1 ) readMeFileName = rootFiles [ i ] ;
2020-11-02 16:13:24 +00:00
files . push ( rootFiles [ i ] . toLowerCase ( ) ) ;
}
2021-01-18 08:53:15 +00:00
if ( files . indexOf ( '.git' ) === - 1 ) throw new Error ( 'No .git folder, aborting' ) ;
2021-01-09 05:08:43 +00:00
prepareRepo ( ) ;
2020-11-02 16:13:24 +00:00
try {
2020-11-23 18:21:51 +00:00
const path = ` ${ pluginPath } /.github/workflows/npmpublish.yml ` ;
2020-11-02 16:13:24 +00:00
if ( ! fs . existsSync ( path ) ) {
2021-01-08 02:52:45 +00:00
console . log ( 'no .github/workflows/npmpublish.yml' ) ;
console . log ( 'create one and set npm secret to auto publish to npm on commit' ) ;
2020-11-02 16:13:24 +00:00
if ( autoFix ) {
const npmpublish =
fs . readFileSync ( 'bin/plugins/lib/npmpublish.yml' , { encoding : 'utf8' , flag : 'r' } ) ;
2020-11-23 18:21:51 +00:00
fs . mkdirSync ( ` ${ pluginPath } /.github/workflows ` , { recursive : true } ) ;
2020-11-02 16:13:24 +00:00
fs . writeFileSync ( path , npmpublish ) ;
console . log ( "If you haven't already, setup autopublish for this plugin https://github.com/ether/etherpad-lite/wiki/Plugins:-Automatically-publishing-to-npm-on-commit-to-Github-Repo" ) ;
} else {
console . log ( 'Setup autopublish for this plugin https://github.com/ether/etherpad-lite/wiki/Plugins:-Automatically-publishing-to-npm-on-commit-to-Github-Repo' ) ;
}
2020-11-23 18:21:51 +00:00
} else {
2020-11-23 16:34:26 +00:00
// autopublish exists, we should check the version..
// checkVersion takes two file paths and checks for a version string in them.
const currVersionFile = fs . readFileSync ( path , { encoding : 'utf8' , flag : 'r' } ) ;
2020-11-23 18:21:51 +00:00
const existingConfigLocation = currVersionFile . indexOf ( '##ETHERPAD_NPM_V=' ) ;
2021-01-08 02:52:45 +00:00
const existingValue = parseInt (
currVersionFile . substr ( existingConfigLocation + 17 , existingConfigLocation . length ) ) ;
2020-11-23 16:34:26 +00:00
2021-01-08 02:52:45 +00:00
const reqVersionFile =
fs . readFileSync ( 'bin/plugins/lib/npmpublish.yml' , { encoding : 'utf8' , flag : 'r' } ) ;
2020-11-23 18:21:51 +00:00
const reqConfigLocation = reqVersionFile . indexOf ( '##ETHERPAD_NPM_V=' ) ;
2021-01-08 02:52:45 +00:00
const reqValue =
parseInt ( reqVersionFile . substr ( reqConfigLocation + 17 , reqConfigLocation . length ) ) ;
2020-11-23 16:34:26 +00:00
2020-11-23 18:21:51 +00:00
if ( ! existingValue || ( reqValue > existingValue ) ) {
2020-11-23 16:34:26 +00:00
const npmpublish =
fs . readFileSync ( 'bin/plugins/lib/npmpublish.yml' , { encoding : 'utf8' , flag : 'r' } ) ;
2020-11-23 18:21:51 +00:00
fs . mkdirSync ( ` ${ pluginPath } /.github/workflows ` , { recursive : true } ) ;
2020-11-23 16:34:26 +00:00
fs . writeFileSync ( path , npmpublish ) ;
}
2020-11-02 16:13:24 +00:00
}
} catch ( err ) {
console . error ( err ) ;
}
2020-11-30 17:22:52 +00:00
try {
const path = ` ${ pluginPath } /.github/workflows/backend-tests.yml ` ;
if ( ! fs . existsSync ( path ) ) {
2021-01-08 02:52:45 +00:00
console . log ( 'no .github/workflows/backend-tests.yml' ) ;
console . log ( 'create one and set npm secret to auto publish to npm on commit' ) ;
2020-11-30 17:22:52 +00:00
if ( autoFix ) {
const backendTests =
fs . readFileSync ( 'bin/plugins/lib/backend-tests.yml' , { encoding : 'utf8' , flag : 'r' } ) ;
fs . mkdirSync ( ` ${ pluginPath } /.github/workflows ` , { recursive : true } ) ;
fs . writeFileSync ( path , backendTests ) ;
}
} else {
// autopublish exists, we should check the version..
// checkVersion takes two file paths and checks for a version string in them.
const currVersionFile = fs . readFileSync ( path , { encoding : 'utf8' , flag : 'r' } ) ;
const existingConfigLocation = currVersionFile . indexOf ( '##ETHERPAD_NPM_V=' ) ;
2021-01-08 02:52:45 +00:00
const existingValue = parseInt (
currVersionFile . substr ( existingConfigLocation + 17 , existingConfigLocation . length ) ) ;
2020-11-30 17:22:52 +00:00
2021-01-08 02:52:45 +00:00
const reqVersionFile =
fs . readFileSync ( 'bin/plugins/lib/backend-tests.yml' , { encoding : 'utf8' , flag : 'r' } ) ;
2020-11-30 17:22:52 +00:00
const reqConfigLocation = reqVersionFile . indexOf ( '##ETHERPAD_NPM_V=' ) ;
2021-01-08 02:52:45 +00:00
const reqValue =
parseInt ( reqVersionFile . substr ( reqConfigLocation + 17 , reqConfigLocation . length ) ) ;
2020-11-30 17:22:52 +00:00
if ( ! existingValue || ( reqValue > existingValue ) ) {
const backendTests =
fs . readFileSync ( 'bin/plugins/lib/backend-tests.yml' , { encoding : 'utf8' , flag : 'r' } ) ;
fs . mkdirSync ( ` ${ pluginPath } /.github/workflows ` , { recursive : true } ) ;
fs . writeFileSync ( path , backendTests ) ;
}
}
} catch ( err ) {
console . error ( err ) ;
}
2020-11-23 18:21:51 +00:00
if ( files . indexOf ( 'package.json' ) === - 1 ) {
console . warn ( 'no package.json, please create' ) ;
2020-11-02 16:13:24 +00:00
}
2020-11-23 18:21:51 +00:00
if ( files . indexOf ( 'package.json' ) !== - 1 ) {
2021-01-08 02:52:45 +00:00
const packageJSON =
fs . readFileSync ( ` ${ pluginPath } /package.json ` , { encoding : 'utf8' , flag : 'r' } ) ;
2020-11-23 18:21:51 +00:00
const parsedPackageJSON = JSON . parse ( packageJSON ) ;
if ( autoFix ) {
let updatedPackageJSON = false ;
if ( ! parsedPackageJSON . funding ) {
2020-11-02 16:13:24 +00:00
updatedPackageJSON = true ;
parsedPackageJSON . funding = {
2020-11-23 18:21:51 +00:00
type : 'individual' ,
2020-11-27 19:55:51 +00:00
url : 'https://etherpad.org/' ,
2020-11-23 18:21:51 +00:00
} ;
2020-11-02 16:13:24 +00:00
}
2020-11-23 18:21:51 +00:00
if ( updatedPackageJSON ) {
2021-01-09 23:27:37 +00:00
writePackageJson ( parsedPackageJSON ) ;
2020-11-02 16:13:24 +00:00
}
}
2020-11-23 18:21:51 +00:00
if ( packageJSON . toLowerCase ( ) . indexOf ( 'repository' ) === - 1 ) {
console . warn ( 'No repository in package.json' ) ;
if ( autoFix ) {
2021-01-08 02:52:45 +00:00
console . warn ( 'Repository not detected in package.json. Add repository section.' ) ;
2020-11-02 16:13:24 +00:00
}
2020-11-23 18:21:51 +00:00
} else {
2020-11-02 16:13:24 +00:00
// useful for creating README later.
repository = parsedPackageJSON . repository . url ;
}
2021-01-09 23:51:31 +00:00
updateDeps ( parsedPackageJSON , 'devDependencies' , {
2021-01-08 23:45:27 +00:00
'eslint' : '^7.17.0' ,
'eslint-config-etherpad' : '^1.0.22' ,
'eslint-plugin-eslint-comments' : '^3.2.0' ,
'eslint-plugin-mocha' : '^8.0.0' ,
'eslint-plugin-node' : '^11.1.0' ,
'eslint-plugin-prefer-arrow' : '^1.2.2' ,
'eslint-plugin-promise' : '^4.2.1' ,
'eslint-plugin-you-dont-need-lodash-underscore' : '^6.10.0' ,
2021-01-09 23:51:31 +00:00
} ) ;
2020-11-22 14:58:11 +00:00
2021-01-10 21:23:04 +00:00
updateDeps ( parsedPackageJSON , 'peerDependencies' , {
// Some plugins require a newer version of Etherpad so don't overwrite if already set.
'ep_etherpad-lite' : { ver : '>=1.8.6' , overwrite : false } ,
} ) ;
2020-11-27 21:43:23 +00:00
2020-11-23 18:21:51 +00:00
if ( packageJSON . toLowerCase ( ) . indexOf ( 'eslintconfig' ) === - 1 ) {
console . warn ( 'No esLintConfig in package.json' ) ;
if ( autoFix ) {
const eslintConfig = {
root : true ,
extends : 'etherpad/plugin' ,
} ;
2020-11-22 14:58:11 +00:00
parsedPackageJSON . eslintConfig = eslintConfig ;
2021-01-09 23:27:37 +00:00
writePackageJson ( parsedPackageJSON ) ;
2020-11-22 14:58:11 +00:00
}
}
2020-11-23 18:21:51 +00:00
if ( packageJSON . toLowerCase ( ) . indexOf ( 'scripts' ) === - 1 ) {
console . warn ( 'No scripts in package.json' ) ;
if ( autoFix ) {
const scripts = {
'lint' : 'eslint .' ,
'lint:fix' : 'eslint --fix .' ,
} ;
2020-11-22 14:58:11 +00:00
parsedPackageJSON . scripts = scripts ;
2021-01-09 23:27:37 +00:00
writePackageJson ( parsedPackageJSON ) ;
2020-11-22 14:58:11 +00:00
}
}
2020-12-16 21:51:43 +00:00
if ( ( packageJSON . toLowerCase ( ) . indexOf ( 'engines' ) === - 1 ) || ! parsedPackageJSON . engines . node ) {
2020-11-27 21:43:23 +00:00
console . warn ( 'No engines or node engine in package.json' ) ;
2020-11-23 18:21:51 +00:00
if ( autoFix ) {
const engines = {
2020-11-27 21:43:23 +00:00
node : '>=10.13.0' ,
2020-11-23 18:21:51 +00:00
} ;
2020-11-22 14:58:11 +00:00
parsedPackageJSON . engines = engines ;
2021-01-09 23:27:37 +00:00
writePackageJson ( parsedPackageJSON ) ;
2020-11-22 14:58:11 +00:00
}
}
2020-11-02 16:13:24 +00:00
}
2020-11-23 18:21:51 +00:00
if ( files . indexOf ( 'package-lock.json' ) === - 1 ) {
2021-01-09 23:50:06 +00:00
console . warn ( 'package-lock.json not found' ) ;
if ( ! autoFix ) {
console . warn ( 'Run npm install in the plugin folder and commit the package-lock.json file.' ) ;
2020-11-02 16:13:24 +00:00
}
}
2020-11-23 18:21:51 +00:00
if ( files . indexOf ( 'readme' ) === - 1 && files . indexOf ( 'readme.md' ) === - 1 ) {
console . warn ( 'README.md file not found, please create' ) ;
if ( autoFix ) {
2021-01-08 02:52:45 +00:00
console . log ( 'Autofixing missing README.md file' ) ;
console . log ( 'please edit the README.md file further to include plugin specific details.' ) ;
2020-11-23 18:21:51 +00:00
let readme = fs . readFileSync ( 'bin/plugins/lib/README.md' , { encoding : 'utf8' , flag : 'r' } ) ;
2020-11-02 16:13:24 +00:00
readme = readme . replace ( /\[plugin_name\]/g , pluginName ) ;
2020-11-23 18:21:51 +00:00
if ( repository ) {
const org = repository . split ( '/' ) [ 3 ] ;
const name = repository . split ( '/' ) [ 4 ] ;
2020-11-02 16:13:24 +00:00
readme = readme . replace ( /\[org_name\]/g , org ) ;
readme = readme . replace ( /\[repo_url\]/g , name ) ;
2020-11-23 18:21:51 +00:00
fs . writeFileSync ( ` ${ pluginPath } /README.md ` , readme ) ;
} else {
console . warn ( 'Unable to find repository in package.json, aborting.' ) ;
2020-11-02 16:13:24 +00:00
}
}
}
2020-12-05 07:47:13 +00:00
if ( files . indexOf ( 'contributing' ) === - 1 && files . indexOf ( 'contributing.md' ) === - 1 ) {
console . warn ( 'CONTRIBUTING.md file not found, please create' ) ;
if ( autoFix ) {
2021-01-18 08:53:15 +00:00
console . log ( 'Autofixing missing CONTRIBUTING.md file, please edit the CONTRIBUTING.md ' +
'file further to include plugin specific details.' ) ;
let contributing =
fs . readFileSync ( 'bin/plugins/lib/CONTRIBUTING.md' , { encoding : 'utf8' , flag : 'r' } ) ;
2020-12-05 07:47:13 +00:00
contributing = contributing . replace ( /\[plugin_name\]/g , pluginName ) ;
fs . writeFileSync ( ` ${ pluginPath } /CONTRIBUTING.md ` , contributing ) ;
}
}
2020-11-23 18:21:51 +00:00
if ( files . indexOf ( 'readme' ) !== - 1 && files . indexOf ( 'readme.md' ) !== - 1 ) {
2021-01-18 08:53:15 +00:00
const readme =
fs . readFileSync ( ` ${ pluginPath } / ${ readMeFileName } ` , { encoding : 'utf8' , flag : 'r' } ) ;
2020-11-23 18:21:51 +00:00
if ( readme . toLowerCase ( ) . indexOf ( 'license' ) === - 1 ) {
console . warn ( 'No license section in README' ) ;
if ( autoFix ) {
console . warn ( 'Please add License section to README manually.' ) ;
2020-11-02 16:13:24 +00:00
}
}
}
2020-11-23 18:21:51 +00:00
if ( files . indexOf ( 'license' ) === - 1 && files . indexOf ( 'license.md' ) === - 1 ) {
console . warn ( 'LICENSE.md file not found, please create' ) ;
if ( autoFix ) {
console . log ( 'Autofixing missing LICENSE.md file, including Apache 2 license.' ) ;
2021-01-09 04:04:11 +00:00
let license = fs . readFileSync ( 'bin/plugins/lib/LICENSE.md' , { encoding : 'utf8' , flag : 'r' } ) ;
license = license . replace ( '[yyyy]' , new Date ( ) . getFullYear ( ) ) ;
license = license . replace ( '[name of copyright owner]' , execSync ( 'git config user.name' ) ) ;
fs . writeFileSync ( ` ${ pluginPath } /LICENSE.md ` , license ) ;
2020-11-02 16:13:24 +00:00
}
}
2020-11-23 18:21:51 +00:00
let travisConfig = fs . readFileSync ( 'bin/plugins/lib/travis.yml' , { encoding : 'utf8' , flag : 'r' } ) ;
2020-11-02 16:13:24 +00:00
travisConfig = travisConfig . replace ( /\[plugin_name\]/g , pluginName ) ;
2020-11-23 18:21:51 +00:00
if ( files . indexOf ( '.travis.yml' ) === - 1 ) {
2021-01-18 08:53:15 +00:00
console . warn ( '.travis.yml file not found, please create. ' +
'.travis.yml is used for automatically CI testing Etherpad. ' +
'It is useful to know if your plugin breaks another feature for example.' ) ;
2020-11-02 16:13:24 +00:00
// TODO: Make it check version of the .travis file to see if it needs an update.
2020-11-23 18:21:51 +00:00
if ( autoFix ) {
console . log ( 'Autofixing missing .travis.yml file' ) ;
fs . writeFileSync ( ` ${ pluginPath } /.travis.yml ` , travisConfig ) ;
console . log ( 'Travis file created, please sign into travis and enable this repository' ) ;
2020-11-02 16:13:24 +00:00
}
}
2021-01-11 00:22:59 +00:00
if ( autoFix ) {
2020-11-02 16:13:24 +00:00
// checks the file versioning of .travis and updates it to the latest.
2021-01-18 08:53:15 +00:00
const existingConfig =
fs . readFileSync ( ` ${ pluginPath } /.travis.yml ` , { encoding : 'utf8' , flag : 'r' } ) ;
2020-11-23 18:21:51 +00:00
const existingConfigLocation = existingConfig . indexOf ( '##ETHERPAD_TRAVIS_V=' ) ;
2021-01-18 08:53:15 +00:00
const existingValue =
parseInt ( existingConfig . substr ( existingConfigLocation + 20 , existingConfig . length ) ) ;
2020-11-23 18:21:51 +00:00
const newConfigLocation = travisConfig . indexOf ( '##ETHERPAD_TRAVIS_V=' ) ;
const newValue = parseInt ( travisConfig . substr ( newConfigLocation + 20 , travisConfig . length ) ) ;
if ( existingConfigLocation === - 1 ) {
console . warn ( 'no previous .travis.yml version found so writing new.' ) ;
2020-11-02 16:13:24 +00:00
// we will write the newTravisConfig to the location.
2020-11-23 18:21:51 +00:00
fs . writeFileSync ( ` ${ pluginPath } /.travis.yml ` , travisConfig ) ;
} else if ( newValue > existingValue ) {
console . log ( 'updating .travis.yml' ) ;
fs . writeFileSync ( ` ${ pluginPath } /.travis.yml ` , travisConfig ) ;
2020-11-28 13:13:14 +00:00
} //
2020-11-02 16:13:24 +00:00
}
2020-11-23 18:21:51 +00:00
if ( files . indexOf ( '.gitignore' ) === - 1 ) {
2021-01-18 08:53:15 +00:00
console . warn ( '.gitignore file not found, please create. .gitignore files are useful to ' +
"ensure files aren't incorrectly commited to a repository." ) ;
2020-11-23 18:21:51 +00:00
if ( autoFix ) {
console . log ( 'Autofixing missing .gitignore file' ) ;
const gitignore = fs . readFileSync ( 'bin/plugins/lib/gitignore' , { encoding : 'utf8' , flag : 'r' } ) ;
fs . writeFileSync ( ` ${ pluginPath } /.gitignore ` , gitignore ) ;
2020-11-02 16:13:24 +00:00
}
2020-12-16 21:51:43 +00:00
} else {
2020-11-28 13:13:14 +00:00
let gitignore =
fs . readFileSync ( ` ${ pluginPath } /.gitignore ` , { encoding : 'utf8' , flag : 'r' } ) ;
2020-12-16 21:51:43 +00:00
if ( gitignore . indexOf ( 'node_modules/' ) === - 1 ) {
console . warn ( 'node_modules/ missing from .gitignore' ) ;
if ( autoFix ) {
gitignore += 'node_modules/' ;
fs . writeFileSync ( ` ${ pluginPath } /.gitignore ` , gitignore ) ;
}
}
2020-11-02 16:13:24 +00:00
}
// if we include templates but don't have translations...
2020-11-23 18:21:51 +00:00
if ( files . indexOf ( 'templates' ) !== - 1 && files . indexOf ( 'locales' ) === - 1 ) {
2021-01-18 08:53:15 +00:00
console . warn ( 'Translations not found, please create. ' +
'Translation files help with Etherpad accessibility.' ) ;
2020-11-02 16:13:24 +00:00
}
2020-11-23 18:21:51 +00:00
if ( files . indexOf ( '.ep_initialized' ) !== - 1 ) {
2021-01-18 08:53:15 +00:00
console . warn (
'.ep_initialized found, please remove. .ep_initialized should never be commited to git ' +
'and should only exist once the plugin has been executed one time.' ) ;
2020-11-23 18:21:51 +00:00
if ( autoFix ) {
console . log ( 'Autofixing incorrectly existing .ep_initialized file' ) ;
fs . unlinkSync ( ` ${ pluginPath } /.ep_initialized ` ) ;
2020-11-02 16:13:24 +00:00
}
}
2020-11-23 18:21:51 +00:00
if ( files . indexOf ( 'npm-debug.log' ) !== - 1 ) {
2021-01-18 08:53:15 +00:00
console . warn ( 'npm-debug.log found, please remove. npm-debug.log should never be commited to ' +
'your repository.' ) ;
2020-11-23 18:21:51 +00:00
if ( autoFix ) {
console . log ( 'Autofixing incorrectly existing npm-debug.log file' ) ;
fs . unlinkSync ( ` ${ pluginPath } /npm-debug.log ` ) ;
2020-11-02 16:13:24 +00:00
}
}
2020-11-23 18:21:51 +00:00
if ( files . indexOf ( 'static' ) !== - 1 ) {
fs . readdir ( ` ${ pluginPath } /static ` , ( errRead , staticFiles ) => {
if ( staticFiles . indexOf ( 'tests' ) === - 1 ) {
console . warn ( 'Test files not found, please create tests. https://github.com/ether/etherpad-lite/wiki/Creating-a-plugin#writing-and-running-front-end-tests-for-your-plugin' ) ;
2020-11-02 16:13:24 +00:00
}
2020-11-23 18:21:51 +00:00
} ) ;
} else {
console . warn ( 'Test files not found, please create tests. https://github.com/ether/etherpad-lite/wiki/Creating-a-plugin#writing-and-running-front-end-tests-for-your-plugin' ) ;
2020-11-02 16:13:24 +00:00
}
2021-01-09 23:50:06 +00:00
// Install dependencies so we can run ESLint. This should also create or update package-lock.json
// if autoFix is enabled.
const npmInstall = ` npm install ${ autoFix ? '' : ' --no-package-lock' } ` ;
execSync ( npmInstall , { stdio : 'inherit' } ) ;
2021-01-10 00:29:30 +00:00
// The ep_etherpad-lite peer dep must be installed last otherwise `npm install` will nuke it. An
// absolute path to etherpad-lite/src is used here so that pluginPath can be a symlink.
execSync (
` ${ npmInstall } --no-save ep_etherpad-lite@file: ${ _ _dirname } /../../src ` , { stdio : 'inherit' } ) ;
2021-01-09 23:50:06 +00:00
2020-11-22 14:58:11 +00:00
// linting begins
2020-11-23 18:21:51 +00:00
try {
2021-01-09 05:59:20 +00:00
console . log ( 'Linting...' ) ;
2021-01-08 02:52:45 +00:00
const lintCmd = autoFix ? 'npx eslint --fix .' : 'npx eslint' ;
2021-01-09 05:59:56 +00:00
execSync ( lintCmd , { stdio : 'inherit' } ) ;
2020-11-23 18:21:51 +00:00
} catch ( e ) {
2020-11-22 14:58:11 +00:00
// it is gonna throw an error anyway
2020-11-23 18:21:51 +00:00
console . log ( 'Manual linting probably required, check with: npm run lint' ) ;
2020-11-22 14:58:11 +00:00
}
// linting ends.
2021-01-10 00:16:56 +00:00
if ( autoFix ) {
const unchanged = JSON . parse ( execSync (
'untracked=$(git ls-files -o --exclude-standard) || exit 1; ' +
'git diff-files --quiet && [ -z "$untracked" ] && echo true || echo false' ) ) ;
if ( ! unchanged ) {
2021-01-10 23:28:32 +00:00
// Display a diff of changes. Git doesn't diff untracked files, so they must be added to the
// index. Use a temporary index file to avoid modifying Git's default index file.
execSync ( 'git read-tree HEAD; git add -A && git diff-index -p --cached HEAD && echo ""' , {
env : { ... process . env , GIT _INDEX _FILE : '.git/checkPlugin.index' } ,
stdio : 'inherit' ,
} ) ;
fs . unlinkSync ( ` ${ pluginPath } /.git/checkPlugin.index ` ) ;
2021-01-10 00:16:56 +00:00
const cmd = [
'git add -A' ,
'git commit -m "autofixes from Etherpad checkPlugin.js"' ,
'git push' ,
] . join ( ' && ' ) ;
if ( autoCommit ) {
console . log ( 'Attempting autocommit and auto publish to npm' ) ;
execSync ( cmd , { stdio : 'inherit' } ) ;
} else {
2021-01-10 23:28:32 +00:00
console . log ( 'Fixes applied. Check the above git diff then run the following command:' ) ;
2021-01-10 00:16:56 +00:00
console . log ( ` (cd node_modules/ ${ pluginName } && ${ cmd } ) ` ) ;
}
2020-11-23 18:21:51 +00:00
} else {
2021-01-10 00:16:56 +00:00
console . log ( 'No changes.' ) ;
2020-11-02 16:13:24 +00:00
}
}
2020-11-23 18:21:51 +00:00
console . log ( 'Finished' ) ;
2020-11-02 16:13:24 +00:00
} ) ;