import { rollup } from "rollup";
import fs from "fs-extra";
import path from "path";
import os from "os";
import copy from "../src";
let TEST_DIR;
beforeEach(async () => {
TEST_DIR = path.join(os.tmpdir(), "rollup-plugin-copy-assets");
await fs.emptyDir(TEST_DIR);
});
afterAll(async () => {
await fs.emptyDir(TEST_DIR);
});
it("should copy the assets to the output dir", async () => {
const BUNDLE_PATH = path.join(TEST_DIR, "bundle.js");
const ASSET_PATH = path.join(TEST_DIR, "top-level-item.txt");
const input = {
input: `${__dirname}/fixtures/index.js`,
plugins: [copy({ assets: ["fixtures/top-level-item.txt"] })],
};
const output = {
file: BUNDLE_PATH,
format: "iife",
name: "test",
};
const bundle = await rollup(input);
await bundle.write(output);
await expect(fs.pathExists(BUNDLE_PATH)).resolves.toEqual(true);
await expect(fs.pathExists(ASSET_PATH)).resolves.toEqual(true);
});
it("should not fail when used with an array of inputs", async () => {
const BUNDLE1_PATH = path.join(TEST_DIR, "index.js");
const BUNDLE2_PATH = path.join(TEST_DIR, "index2.js");
const ASSET_PATH = path.join(TEST_DIR, "top-level-item.txt");
const input = {
input: [
path.join(__dirname, "fixtures", "index.js"),
path.join(__dirname, "fixtures", "index2.js"),
],
plugins: [copy({ assets: ["fixtures/top-level-item.txt"] })],
};
const output = {
dir: TEST_DIR,
format: "cjs",
};
const bundle = await rollup(input);
await bundle.write(output);
await expect(fs.pathExists(BUNDLE1_PATH)).resolves.toEqual(true);
await expect(fs.pathExists(BUNDLE2_PATH)).resolves.toEqual(true);
await expect(fs.pathExists(ASSET_PATH)).resolves.toEqual(true);
});
it("should not fail when used with an object of inputs", async () => {
const BUNDLE1_PATH = path.join(TEST_DIR, "index.js");
const BUNDLE2_PATH = path.join(TEST_DIR, "index2.js");
const ASSET_PATH = path.join(TEST_DIR, "top-level-item.txt");
const input = {
input: {
index: path.join(__dirname, "fixtures", "index.js"),
index2: path.join(__dirname, "fixtures", "index2.js"),
},
plugins: [copy({ assets: ["fixtures/top-level-item.txt"] })],
};
const output = {
dir: TEST_DIR,
format: "cjs",
};
const bundle = await rollup(input);
await bundle.write(output);
await expect(fs.pathExists(BUNDLE1_PATH)).resolves.toEqual(true);
await expect(fs.pathExists(BUNDLE2_PATH)).resolves.toEqual(true);
await expect(fs.pathExists(ASSET_PATH)).resolves.toEqual(true);
});
it("should copy directories of assets", async () => {
const BUNDLE_PATH = path.join(TEST_DIR, "bundle.js");
const TOP_LEVEL_ASSET = path.join(TEST_DIR, "top-level-item.txt");
const ASSET_FOLDER = path.join(TEST_DIR, "assets");
const CSV_ASSET = path.join(ASSET_FOLDER, "bar.csv");
const TXT_ASSET = path.join(ASSET_FOLDER, "foo.txt");
await expect(fs.pathExists(BUNDLE_PATH)).resolves.toEqual(false);
await expect(fs.pathExists(TOP_LEVEL_ASSET)).resolves.toEqual(false);
await expect(fs.pathExists(CSV_ASSET)).resolves.toEqual(false);
await expect(fs.pathExists(TXT_ASSET)).resolves.toEqual(false);
const input = {
input: path.join(__dirname, "fixtures", "index.js"),
plugins: [
copy({ assets: ["fixtures/assets", "fixtures/top-level-item.txt"] }),
],
};
const output = {
file: BUNDLE_PATH,
format: "iife",
name: "test",
};
const bundle = await rollup(input);
await bundle.write(output);
await expect(fs.pathExists(BUNDLE_PATH)).resolves.toEqual(true);
await expect(fs.pathExists(TOP_LEVEL_ASSET)).resolves.toEqual(true);
await expect(fs.pathExists(CSV_ASSET)).resolves.toEqual(true);
await expect(fs.pathExists(TXT_ASSET)).resolves.toEqual(true);
});
it("should not fail when an asset or directory already exists", async () => {
const BUNDLE_PATH = path.join(TEST_DIR, "bundle.js");
const ASSET_FOLDER = path.join(TEST_DIR, "assets");
const TOP_LEVEL_ASSET = path.join(TEST_DIR, "top-level-item.txt");
const CSV_ASSET = path.join(ASSET_FOLDER, "bar.csv");
const TXT_ASSET = path.join(ASSET_FOLDER, "foo.txt");
// Create all of the files so they exist
await fs.ensureFile(BUNDLE_PATH);
await fs.ensureFile(TOP_LEVEL_ASSET);
await fs.ensureFile(CSV_ASSET);
await fs.ensureFile(TXT_ASSET);
await expect(fs.pathExists(BUNDLE_PATH)).resolves.toEqual(true);
await expect(fs.pathExists(TOP_LEVEL_ASSET)).resolves.toEqual(true);
await expect(fs.pathExists(CSV_ASSET)).resolves.toEqual(true);
await expect(fs.pathExists(TXT_ASSET)).resolves.toEqual(true);
const input = {
input: path.join(__dirname, "fixtures", "index.js"),
plugins: [
copy({ assets: ["fixtures/assets", "fixtures/top-level-item.txt"] }),
],
};
const output = {
file: BUNDLE_PATH,
format: "iife",
name: "test",
};
const bundle = await rollup(input);
await bundle.write(output);
await expect(fs.pathExists(BUNDLE_PATH)).resolves.toEqual(true);
await expect(fs.pathExists(TOP_LEVEL_ASSET)).resolves.toEqual(true);
await expect(fs.pathExists(CSV_ASSET)).resolves.toEqual(true);
await expect(fs.pathExists(TXT_ASSET)).resolves.toEqual(true);
});
/* eslint-env mocha */
const chai = require('chai')
const path = require('path')
const fs = require('fs-extra')
const os = require('os')
const proxyquire = require('proxyquire')
const Logger = require('../../../src/utils/logger')
const expect = chai.expect
const temp = os.tmpdir()
const modulePath = '../../../src/utils/create-new-project'
const createNewProject = require(modulePath)
const defaultOptions = {
skipGit: true,
skipInstall: true,
quiet: true,
logger: new Logger({ quiet: true })
}
let appName, appPath
describe('utils/create-new-project', () => {
beforeEach(() => {
appName = `test-${Date.now()}`
appPath = path.join(temp, appName)
})
it('should create an app project from the template', async () => {
await createNewProject(temp, 'app', appName, defaultOptions)
expect(await fs.pathExists(appPath)).to.equal(true)
const packageInfo = await fs.readJson(path.join(appPath, 'package.json'))
expect(packageInfo.name).to.equal(appName)
const koopConfig = await fs.readJson(path.join(appPath, 'koop.json'))
expect(koopConfig.type).to.equal('app')
expect(koopConfig.plugins).to.be.an('Array')
expect(await fs.pathExists(path.join(appPath, 'src/index.js'))).to.equal(true)
expect(await fs.pathExists(path.join(appPath, 'src/routes.js'))).to.equal(true)
expect(await fs.pathExists(path.join(appPath, 'src/plugins.js'))).to.equal(true)
expect(await fs.pathExists(path.join(appPath, 'src/request-handlers/welcome-page.js'))).to.equal(true)
})
it('should create a provider project from the template', async () => {
await createNewProject(temp, 'provider', appName, defaultOptions)
expect(await fs.pathExists(appPath)).to.equal(true)
const packageInfo = await fs.readJson(path.join(appPath, 'package.json'))
expect(packageInfo.name).to.equal(appName)
const koopConfig = await fs.readJson(path.join(appPath, 'koop.json'))
expect(koopConfig.type).to.equal('provider')
expect(koopConfig.name).to.be.a('string')
expect(koopConfig.allowedParams).to.be.an('object')
expect(await fs.pathExists(path.join(appPath, 'src/index.js'))).to.equal(true)
expect(await fs.pathExists(path.join(appPath, 'src/model.js'))).to.equal(true)
expect(await fs.pathExists(path.join(appPath, 'test/index.test.js'))).to.equal(true)
expect(await fs.pathExists(path.join(appPath, 'test/model.test.js'))).to.equal(true)
})
it('should create an auth plugin project from the template', async () => {
await createNewProject(temp, 'auth', appName, defaultOptions)
expect(await fs.pathExists(appPath)).to.equal(true)
const packageInfo = await fs.readJson(path.join(appPath, 'package.json'))
expect(packageInfo.name).to.equal(appName)
const koopConfig = await fs.readJson(path.join(appPath, 'koop.json'))
expect(koopConfig.type).to.equal('auth')
expect(await fs.pathExists(path.join(appPath, 'src/index.js'))).to.equal(true)
expect(await fs.pathExists(path.join(appPath, 'src/authenticate.js'))).to.equal(true)
expect(await fs.pathExists(path.join(appPath, 'src/authorize.js'))).to.equal(true)
expect(await fs.pathExists(path.join(appPath, 'src/authentication-specification.js'))).to.equal(true)
expect(await fs.pathExists(path.join(appPath, 'test/index.test.js'))).to.equal(true)
expect(await fs.pathExists(path.join(appPath, 'test/authenticate.test.js'))).to.equal(true)
expect(await fs.pathExists(path.join(appPath, 'test/authentication-specification.test.js'))).to.equal(true)
expect(await fs.pathExists(path.join(appPath, 'test/data.geojson'))).to.equal(true)
})
it('should create an output plugin project from the template', async () => {
await createNewProject(temp, 'output', appName, defaultOptions)
expect(await fs.pathExists(appPath)).to.equal(true)
const packageInfo = await fs.readJson(path.join(appPath, 'package.json'))
expect(packageInfo.name).to.equal(appName)
const koopConfig = await fs.readJson(path.join(appPath, 'koop.json'))
expect(koopConfig.type).to.equal('output')
expect(await fs.pathExists(path.join(appPath, 'src/index.js'))).to.equal(true)
expect(await fs.pathExists(path.join(appPath, 'src/routes.js'))).to.equal(true)
expect(await fs.pathExists(path.join(appPath, 'src/request-handlers/serve.js'))).to.equal(true)
expect(await fs.pathExists(path.join(appPath, 'test/index.test.js'))).to.equal(true)
expect(await fs.pathExists(path.join(appPath, 'test/routes.test.js'))).to.equal(true)
expect(await fs.pathExists(path.join(appPath, 'test/request-handlers/serve.test.js'))).to.equal(true)
expect(await fs.pathExists(path.join(appPath, 'test/data.geojson'))).to.equal(true)
})
it('should update the config file if the config is specified with a JSON', async () => {
await createNewProject(
temp,
'app',
appName,
{
...defaultOptions,
config: { port: 3000 }
}
)
const configPath = path.join(appPath, 'config/default.json')
expect(await fs.pathExists(configPath)).to.equal(true)
const config = await fs.readJson(configPath)
expect(config.port).to.equal(3000)
})
it('should set the npm client if specified', async () => {
await createNewProject(
temp,
'app',
appName,
{
...defaultOptions,
npmClient: 'yarn'
}
)
const configPath = path.join(appPath, 'koop.json')
const config = await fs.readJson(configPath)
expect(config.npmClient).to.equal('yarn')
})
it('should set the deployment addon files if the deployment targe is specified', async () => {
const createMock = proxyquire(modulePath, {
'./add-deployment-target': (cwd, options) => {
expect(options.deploymentTarget).to.equal('docker')
}
})
await createMock(
temp,
'app',
appName,
{
...defaultOptions,
deploymentTarget: 'docker'
}
)
})
})
const path = require('path');
const fs = require('fs-extra');
const _Directorys = require('./_directorys');
const sleep = require('./_sleep');
const createDirectory = async (directoryToMake) => {
await sleep(250);
if (await fs.pathExists(directoryToMake) === false) await fs.mkdir(directoryToMake);
};
const createRecursiveDirectory = async (directoriesToMake) => {
try {
await sleep(250);
for (const dir of directoriesToMake) await createDirectory(dir);
} catch (err) {
return console.error(err);
}
};
const checkWorkingDirectory = async () => {
await sleep(250);
if (await fs.pathExists(_Directorys.shopRoot) === true
&& await fs.pathExists(_Directorys.productionRoot) === true
&& await fs.pathExists(_Directorys.developmentRoot) === true
&& await fs.pathExists(_Directorys.devRoot) === true
&& await fs.pathExists(_Directorys.scriptsRoot) === true
&& await fs.pathExists(_Directorys.scriptsModuleRoot) === true
&& await fs.pathExists(_Directorys.stylesRoot) === true
&& await fs.pathExists(_Directorys.fontsRoot) === true
&& await fs.pathExists(_Directorys.imagesRoot) === true) return true;
};
const checkDistDirectory = async () => {
await sleep(250);
if (await fs.pathExists(_Directorys.shopRoot) === true
&& await fs.pathExists(_Directorys.productionRoot) === true
&& await fs.pathExists(_Directorys.distAssetsRoot) === true
&& await fs.pathExists(_Directorys.distConfigRoot) === true
&& await fs.pathExists(_Directorys.distLayoutRoot) === true
&& await fs.pathExists(_Directorys.distLocalesRoot) === true
&& await fs.pathExists(_Directorys.distSectionsRoot) === true
&& await fs.pathExists(_Directorys.distSnippetsRoot) === true
&& await fs.pathExists(_Directorys.distTemplatesRoot) === true) return true;
};
const cloneDirectory = async (directoryToCopy = _Directorys.productionRoot, directoryDestination = _Directorys.developmentRoot) => {
await sleep(250);
await fs.copy(directoryToCopy, directoryDestination);
};
const moveFile = async (fileToMove, fileDestination) => {
await sleep(250);
await fs.move(fileToMove, `${fileDestination}/${path.basename(fileToMove)}`)
};
module.exports = {
createDirectory,
createRecursiveDirectory,
checkWorkingDirectory,
checkDistDirectory,
cloneDirectory,
moveFile
};
Accelerate Your Automation Test Cycles With LambdaTest
Leverage LambdaTestās cloud-based platform to execute your automation tests in parallel and trim down your test execution time significantly. Your first 100 automation testing minutes are on us.