Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
export const ensurePackageJSON: AsyncAction = async ({
state,
actions,
effects,
}) => {
const sandbox = state.editor.currentSandbox;
const existingPackageJson = sandbox.modules.find(
module => module.directoryShortid == null && module.title === 'package.json'
);
if (sandbox.owned && !existingPackageJson) {
const optimisticId = effects.utils.createOptimisticId();
const optimisticModule = createOptimisticModule({
id: optimisticId,
title: 'package.json',
code: generatePackageJsonFromSandbox(sandbox),
path: '/package.json',
});
state.editor.currentSandbox.modules.push(optimisticModule as Module);
optimisticModule.path = getModulePath(
sandbox.modules,
sandbox.directories,
optimisticId
);
// We grab the module from the state to continue working with it (proxy)
const module = sandbox.modules[sandbox.modules.length - 1];
effects.vscode.sandboxFsSync.writeFile(state.editor.modulesByPath, module);
try {
.then(x => {
const moduleObject = {};
// We convert the modules to a format the manager understands
x.data.modules.forEach(m => {
const path = getModulePath(x.data.modules, x.data.directories, m.id);
moduleObject[path] = {
path,
code: m.code,
};
});
if (!moduleObject['/package.json']) {
moduleObject['/package.json'] = {
code: generateFileFromSandbox(x.data),
path: '/package.json',
};
}
const data = {
sandboxId: id,
modules: moduleObject,
entry: '/' + x.data.entry,
externalResources: x.data.externalResources,
dependencies: x.data.npmDependencies,
hasActions: false,
template: x.data.template,
version: 3,
disableDependencyPreprocessing: document.location.search.includes(
'csb-dynamic-download'
),
.then(x => {
const moduleObject = {};
// We convert the modules to a format the manager understands
x.data.modules.forEach(m => {
const path = getModulePath(x.data.modules, x.data.directories, m.id);
moduleObject[path] = {
path,
code: m.code,
};
});
if (!moduleObject['/package.json']) {
moduleObject['/package.json'] = {
code: generateFileFromSandbox(x.data),
path: '/package.json',
};
}
const data = {
sandboxId: id,
modules: moduleObject,
entry: '/' + x.data.entry,
externalResources: x.data.externalResources,
dependencies: x.data.npmDependencies,
hasActions: false,
template: x.data.template,
version: 3,
disableDependencyPreprocessing: document.location.search.includes(
'csb-dynamic-download'
),
sandbox.modules.forEach(m => {
const { path } = m;
if (path) {
modulesObject[path] = {
code: m.code,
savedCode: m.savedCode,
path,
isBinary: m.isBinary,
};
}
});
if (!modulesObject['/package.json']) {
modulesObject['/package.json'] = {
code: generateFileFromSandbox(sandbox),
savedCode: null,
path: '/package.json',
isBinary: false,
};
}
return modulesObject;
}
export function createPackageJSON({ props }) {
const { sandbox } = props;
const code = generateFileFromSandbox(sandbox);
return {
title: 'package.json',
newCode: code,
};
}
currentPackageJSONCode: ({ currentSandbox, currentPackageJSON }) => {
if (!currentPackageJSON) {
return null;
}
return currentPackageJSON.code
? currentPackageJSON.code
: generateFileFromSandbox(currentSandbox);
},
shouldDirectoryBeOpen: ({
export function currentPackageJSONCode() {
return this.currentPackageJSON
? this.currentPackageJSON.code
: generateFileFromSandbox(this.currentSandbox);
}