File: plugin.js

package info (click to toggle)
ckeditor 4.16.0%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 258,804 kB
  • sloc: javascript: 239,590; sh: 184; makefile: 64; python: 37; php: 15; xml: 5
file content (392 lines) | stat: -rw-r--r-- 11,839 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
/**
 * @license Copyright (c) 2003-2021, CKSource - Frederico Knabben. All rights reserved.
 * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
 */

/* jshint node: true, browser: false, es3: false */

'use strict';

var fs = require( 'fs' ),
	shjs = require( 'shelljs' );

shjs.config.silent = true;

module.exports = function( grunt ) {
	grunt.registerTask( 'plugin-install', 'Installs external plugin to the plugins/ directory.', function( pluginName ) {
		var installationDir = grunt.config.get( 'plugin.installationDir' ),
			externalPluginDir = getExternalPluginDir( pluginName ),
			installationPluginDir = installationDir + pluginName;

		if ( !pluginName ) {
			grunt.fail.fatal(
				'Name of the plugin must be specified.\n' +
				'Use: grunt plugin-install:<pluginName>'
			);
		}
		assertExternalPluginDir( pluginName );
		if ( isPluginInstalled( pluginName ) ) {
			grunt.log.writeln( 'The "' + pluginName + '" plugin is already installed.' );
			return;
		}

		try {
			grunt.log.writeln( 'Creating a symlink...' );
			fs.symlinkSync( '../' + externalPluginDir, installationPluginDir, 'dir' );

			grunt.log.writeln( 'Adding the plugin to files ignored by Git...' );
			addPluginDirToGitExclude( installationPluginDir );
		} catch ( e ) {
			grunt.log.error( e.message );
			grunt.fail.fatal( 'Error when installing the plugin.' );
		}

		grunt.log.ok( 'Installed the plugin "' + pluginName + '" in "' + installationPluginDir + '" directory.' );
	} );

	grunt.registerTask( 'plugin-uninstall', 'Uninstalls external plugin from the plugins/ directory.', function( pluginName ) {
		var installationDir = grunt.config.get( 'plugin.installationDir' ),
			installationPluginDir = installationDir + pluginName;

		if ( !pluginName ) {
			grunt.fail.fatal(
				'Name of the plugin must be specified.\n' +
				'Use: grunt plugin-uninstall:<pluginName>'
			);
		}
		if ( !isPluginInstalled( pluginName ) ) {
			grunt.log.writeln( 'The "' + pluginName + '" plugin is not installed.' );
			return;
		}
		if ( !isSymlink( installationPluginDir ) ) {
			grunt.fail.warn( 'The "' + installationPluginDir + '" directory is not a symlink so plugin cannot be removed.' );
		}

		try {
			grunt.log.writeln( 'Removing a symlink...' );
			fs.unlinkSync( installationPluginDir );

			grunt.log.writeln( 'Remove the plugin from files ignored by Git...' );
			removePluginDirFromGitExclude( installationPluginDir );
		} catch ( e ) {
			grunt.log.error( e.message );
			grunt.fail.fatal( 'Error when uninstalling the plugin.' );
		}

		grunt.log.ok( 'Uninstalled the plugin "' + pluginName + '" from "' + installationPluginDir + '" directory.' );
	} );

	grunt.registerTask( 'plugin-update', 'Updates plugin to the commit ref/branch/tag specified in package.json.', function( pluginName ) {
		if ( !pluginName ) {
			grunt.fail.fatal(
				'Name of the plugin must be specified.\n' +
				'Use: grunt plugin-update:<pluginName>'
			);
		}
		assertExternalPluginDir( pluginName );

		var pluginConfig = getPluginConfig( pluginName );
		if ( !pluginConfig ) {
			grunt.fail.warn( 'Plugin "' + pluginName + '" is not configured in the package.json file.' );
		}

		grunt.log.writeln( 'Updating plugin "' + pluginName + '" to "' + pluginConfig.remote + '/' + pluginConfig.version + '"...' );

		shjs.pushd( getExternalPluginDir( pluginName ) );

		try {
			// Kinda weak way to check the status. But let's see if it does the job for now.
			if ( gitStatus() ) {
				grunt.log.error(
					'Cannot update the plugin "' + pluginName + '" due to messed status ' +
					'(e.g. uncommited changes or untracked files).'
				);
				return;
			}

			grunt.log.writeln( 'Fetching changes from the remote "' + pluginConfig.remote + '"...' );
			gitFetch( pluginConfig.remote );

			// We could use `git show-ref --verify refs/<tags|heads>/<version>`
			// but KISS...
			if ( !pluginConfig.isCommit ) {
				grunt.log.writeln( 'Reseting to "' + pluginConfig.remote + '/' + pluginConfig.version + '"...' );
				gitCheckout( pluginConfig.version );
				gitResetHard( pluginConfig.remote, pluginConfig.version );
			} else {
				grunt.log.writeln( 'Checking out "' + pluginConfig.version + '"...' );
				gitCheckout( pluginConfig.version );
			}
		} catch ( e ) {
			grunt.log.error( e.message );
			grunt.fail.fatal( 'Error when updating the plugin "' + pluginName + '".' );
		} finally {
			shjs.popd();
		}

		grunt.log.ok( 'Updated the plugin "' + pluginName + '".' );
	} );

	// TODO Add information about branch or tag to which the plugin is checked out.
	grunt.registerTask( 'plugins-list', 'Lists all installed external plugins.', function() {
		assertExternalDir();

		var installedPlugins = getExternalPlugins().filter( isPluginInstalled );
		if ( !installedPlugins.length ) {
			grunt.log.writeln( 'There are no external plugins installed.' );
			return;
		}
		grunt.log.writeln( 'Installed plugins:' );
		grunt.log.writeln( '* ' + installedPlugins.join( '\n* ' ) );
	} );

	grunt.registerTask( 'plugins-list-all', 'Lists all available external plugins.', function() {
		assertExternalDir();

		var plugins = getExternalPlugins();
		if ( !plugins.length ) {
			grunt.log.writeln( 'There are no external plugins available.' );
			return;
		}
		grunt.log.writeln( 'Available plugins:' );
		grunt.log.writeln( '* ' + plugins.join( '\n* ' ) );
	} );

	grunt.registerTask( 'plugins-install', 'Installs all external plugins specified in package.json to the plugins/ directory.', function() {
		var pluginsToInstall = getPluginsNamesFromConfig();

		if ( !pluginsToInstall.length ) {
			grunt.log.writeln( 'There are no plugins configured in the package.json file.' );
			return;
		}

		grunt.log.writeln( 'Installing plugins: ' + pluginsToInstall.join( ', ' ) + '...' );
		grunt.task.run( pluginsToInstall.map( function( pluginName ) {
			return 'plugin-install:' + pluginName;
		} ) );
	} );

	grunt.registerTask( 'plugins-uninstall', 'Uninstalls all external plugins from the plugins/ directory.', function() {
		assertExternalDir();

		var pluginsToUninstall = getExternalPlugins().filter( isPluginInstalled );

		if ( !pluginsToUninstall.length ) {
			grunt.log.writeln( 'There are no external plugins installed.' );
			return;
		}

		grunt.log.writeln( 'Uninstalling plugins: ' + pluginsToUninstall.join( ', ' ) + '...' );
		grunt.task.run( pluginsToUninstall.map( function( pluginName ) {
			return 'plugin-uninstall:' + pluginName;
		} ) );
	} );

	grunt.registerTask( 'plugins-update',
		'Updates installed external plugins to the versions specified in package.json. ' +
		'When used with the :install option the task will also install the plugins specified in package.json.', function( install ) {
		assertExternalDir();

		var installedPlugins = getExternalPlugins().filter( isPluginInstalled ),
			pluginsToInstall = getPluginsNamesFromConfig(),
			tasks = [];

		if ( install ) {
			// Uinstall plugins which are not specified in package.json.
			tasks = tasks.concat(
				installedPlugins
					.filter( function( pluginName ) {
						return pluginsToInstall.indexOf( pluginName ) == -1;
					} )
					.map( function( pluginName ) {
						return 'plugin-uninstall:' + pluginName;
					} )
			);

			// Install plugins which are not yet installed.
			tasks = tasks.concat(
				pluginsToInstall
					.filter( function( pluginName ) {
						return installedPlugins.indexOf( pluginName ) == -1;
					} )
					.map( function( pluginName ) {
						return 'plugin-install:' + pluginName;
					} )
			);

			// Finally, update all installed plugins.
			// We could use the --install parameter instead of :install, but then we would
			// need to use grunt.option( 'install', false ) before calling plugins-update recursively ;/.
			tasks.push( 'plugins-update' );

			grunt.task.run( tasks );
			return;
		}


		if ( !installedPlugins.length ) {
			grunt.log.writeln( 'There are no external plugins installed.' );
			return;
		} else {
			grunt.log.writeln( 'Updating plugins: ' + installedPlugins.join( ', ' ) + '...' );
		}

		grunt.task.run( installedPlugins.map( function( pluginName ) {
			return 'plugin-update:' + pluginName;
		} ) );
	} );


	// Grunt.file.* methods does not understand symlinks, neither fs.exists, so using the lstat.
	function pathExists( path ) {
		try {
			fs.lstatSync( path );
		} catch ( e ) {
			return false;
		}
		return true;
	}

	function isSymlink( path ) {
		try {
			return fs.lstatSync( path ).isSymbolicLink();
		} catch ( e ) {
			return false;
		}
	}

	function addPluginDirToGitExclude( pluginDir ) {
		var excludedPaths = getGitExclude();

		if ( excludedPaths.indexOf( pluginDir ) > -1 ) {
			return;
		}

		excludedPaths.push( pluginDir );

		grunt.file.write( '.git/info/exclude', excludedPaths.join( '\n' ) + '\n' );
	}

	function removePluginDirFromGitExclude( pluginDir ) {
		var excludedPaths = getGitExclude(),
			index = excludedPaths.indexOf( pluginDir );

		if ( index < 0 ) {
			return;
		}

		excludedPaths.splice( index, 1 );

		grunt.file.write( '.git/info/exclude', excludedPaths.join( '\n' ) + '\n' );
	}

	function getGitExclude() {
		return grunt.file.read( '.git/info/exclude' ).toString()
			// Remove empty lines from the end of the file.
			.replace( /\n+$/g, '' )
			.split( /\n/ );
	}

	// Returns an array of directories names inside dir.
	function getDirs( dir ) {
		var fileNames = fs.readdirSync( dir );

		return fileNames.filter( function( fileName ) {
			return fs.lstatSync( dir + fileName ).isDirectory();
		} );
	}

	// Returns an array of external plugins names.
	function getExternalPlugins() {
		return getDirs( grunt.config.get( 'plugin.externalDir' ) );
	}

	// Checks whether a plugin is installed.
	function isPluginInstalled( pluginName ) {
		return pathExists( grunt.config.get( 'plugin.installationDir' ) + pluginName );
	}

	// Checks that the external plugins directory exists.
	function assertExternalDir() {
		var externalDir = grunt.config.get( 'plugin.externalDir' );

		if ( !grunt.file.isDir( externalDir ) ) {
			grunt.fail.fatal( 'The "' + externalDir + '" directory must exist.' );
		}
	}

	function assertExternalPluginDir( pluginName ) {
		var externalPluginDir = getExternalPluginDir( pluginName );

		if ( !grunt.file.isDir( externalPluginDir ) ) {
			grunt.fail.fatal( 'The "' + externalPluginDir + '" directory must exist.' );
		}
	}

	function getExternalPluginDir( pluginName ) {
		return grunt.config.get( 'plugin.externalDir' ) + pluginName;
	}

	function getPluginConfig( pluginName ) {
		var pluginsConfig = grunt.config.get( 'pkg.ckeditorPlugins' );

		if ( pluginsConfig && ( pluginName in pluginsConfig ) ) {
			var pluginConfig = pluginsConfig[ pluginName ];

			// Normalization. Default version and default origin.
			if ( typeof pluginConfig != 'object' ) {
				pluginConfig = {
					version: pluginConfig
				};
			}
			if ( typeof pluginConfig.version != 'string' ) {
				pluginConfig.version = 'master';
			}
			if ( !pluginConfig.remote ) {
				pluginConfig.remote = 'origin';
			}

			return pluginConfig;
		} else {
			return null;
		}
	}

	// Returns names of plugins specified in the package.json.
	function getPluginsNamesFromConfig() {
		var plugins = grunt.config.get( 'pkg.ckeditorPlugins' );

		if ( !plugins )
			return [];

		return Object.keys( plugins );
	}

	function shExec( cmd ) {
		var ret = shjs.exec( cmd );

		if ( ret.code ) {
			throw new Error(
				'Error while executing `' + cmd + '`:\n\n' +
				ret.output
			);
		}
		return ret.output;
	}

	function gitStatus() {
		return shExec( 'git status --porcelain' );
	}

	function gitFetch( remote ) {
		return shExec( 'git fetch ' + remote );
	}

	function gitCheckout( ref ) {
		return shExec( 'git checkout ' + ref );
	}

	function gitResetHard( remote, ref ) {
		return shExec( 'git reset --hard ' + remote + '/' + ref );
	}
};