• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

ckeditor / ckeditor5-package-generator / #746

pending completion
#746

travis-ci

117 of 117 branches covered (100.0%)

Branch coverage included in aggregate %.

416 of 416 relevant lines covered (100.0%)

8.97 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

100.0
/packages/ckeditor5-package-generator/lib/utils/copy-files.js
1
/**
2
 * @license Copyright (c) 2020-2025, CKSource Holding sp. z o.o. All rights reserved.
3
 * For licensing, see LICENSE.md.
4
 */
5

6
import chalk from 'chalk';
7
import fs from 'node:fs';
8
import { globSync } from 'glob';
11✔
9
import mkdirp from 'mkdirp';
11✔
10
import upath from 'upath';
11✔
11
import { template } from 'lodash-es';
11✔
12

11✔
13
const TEMPLATE_PATH = upath.join( import.meta.dirname, '..', 'templates' );
11✔
14

15
/**
11✔
16
 * If the package name is not valid, prints the error and exits the process.
17
 *
18
 * @param {Logger} logger
19
 * @param {Object} options
20
 * @param {String} options.packageName
21
 * @param {FormattedNames} options.formattedNames
22
 * @param {String} options.directoryPath
23
 * @param {String} options.packageManager
24
 * @param {String} options.programmingLanguage
25
 * @param {Object} options.packageVersions
26
 * @param {String} options.installationMethodOfPackage
27
 * @param {String} options.validatedGlobalName
28
 */
29
export default function copyFiles( logger, options ) {
11✔
30
        logger.process( 'Copying files...' );
10✔
31

32
        const supportsLegacyMethods = options.installationMethodOfPackage !== 'current';
10✔
33
        const templatePatternToCopy = `${ options.programmingLanguage }${ supportsLegacyMethods ? '-legacy' : '' }/**/*`;
34

35
        const templateGlobs = [
36
                'common/**/*',
37
                templatePatternToCopy
10✔
38
        ];
20✔
39

40
        const templatesToCopy = templateGlobs
41
                .flatMap( globPattern => {
42
                        return globSync( globPattern, {
43
                                cwd: TEMPLATE_PATH,
44
                                dot: true,
45
                                nodir: true
10✔
46
                        } );
42✔
47
                } )
48
                .filter( file => options.packageManager !== 'pnpm' ? !file.includes( 'pnpm-workspace.yaml' ) : true );
42✔
49

42✔
50
        for ( const templatePath of templatesToCopy ) {
51
                logger.verboseInfo( `* Copying "${ chalk.gray( templatePath ) }"...` );
52

53
                const data = {
54
                        cliSeparator: options.packageManager === 'npm' ? '-- ' : '',
42✔
55
                        now: new Date(),
56
                        ...options
57
                };
58

59
                copyTemplate( templatePath, options.directoryPath, data );
60
        }
61
}
62

63
/**
64
 * Copies all files into the package directory. If any file has any template placeholders, they are filled.
65
 *
66
 * @param {String} templatePath The relative path to the "templates/" directory of the file to copy.
42✔
67
 * @param {String} packagePath The destination directory where the new package is created.
42✔
68
 * @param {Object} data The data to fill in the template file.
69
 */
42✔
70
function copyTemplate( templatePath, packagePath, data ) {
71
        const rawFile = fs.readFileSync( upath.join( TEMPLATE_PATH, templatePath ), 'utf-8' );
72
        const filledFile = template( rawFile )( data );
73

74
        const processedTemplatePath = templatePath
75
                // Remove sub-directory inside templates to merge results into one directory.
76
                .replace( /^(?:common|js|ts|js-legacy|ts-legacy)(?:\\|\/)/, '' )
77
                // We use the ".txt" file extension to circumvent syntax errors in templates and npm not publishing the ".gitignore" file.
42✔
78
                .replace( /\.txt$/, '' )
79
                // Replace placeholder filenames with the class name.
80
                .replace( /_PLACEHOLDER_/, data.formattedNames.plugin.lowerCaseMerged );
42✔
81

42✔
82
        const destinationPath = upath.join( packagePath, processedTemplatePath );
83

84
        // Make sure that the destination directory exists.
85
        mkdirp.sync( upath.dirname( destinationPath ) );
86
        fs.writeFileSync( destinationPath, filledFile );
87
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc