Skip to content

Commit

Permalink
Merge pull request #1525 from polywrap/templates/fix-create-app
Browse files Browse the repository at this point in the history
cli/create: remove node & react from supported langs in app template
  • Loading branch information
dOrgJelli authored Feb 11, 2023
2 parents 38650a9 + ed10b5e commit 7a349ea
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 93 deletions.
4 changes: 2 additions & 2 deletions packages/cli/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -176,8 +176,8 @@ polywrap create wasm assemblyscript my-wrapper
# Create an interface project using assemblyscript called "my-project"
polywrap create wasm interface my-interface

# Create a React app project using Typescript called "my-react-app"
polywrap create app typescript-react my-react-app
# Create a project using Typescript called "my-react-app"
polywrap create app typescript my-app

# Create a Plugin wrapper project using Typescript called "my-plugin"
polywrap create plugin typescript my-plugin
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/src/__tests__/e2e/create.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ Commands:
wasm [options] <language> <name> Create a Polywrap wasm wrapper langs:
assemblyscript, rust, interface
app [options] <language> <name> Create a Polywrap application langs:
typescript-node, typescript-react
typescript
plugin [options] <language> <name> Create a Polywrap plugin langs:
typescript
help [command] display help for command
Expand Down
39 changes: 19 additions & 20 deletions packages/cli/src/commands/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const pathStr = intlMsg.commands_create_options_o_path();

export const supportedLangs = {
wasm: ["assemblyscript", "rust", "interface"] as const,
app: ["typescript-node", "typescript-react"] as const,
app: ["typescript"] as const,
plugin: ["typescript"] as const,
};

Expand Down Expand Up @@ -174,24 +174,23 @@ async function run(
}
}

await generateProjectTemplate(command, language, projectDir)
.then(() => {
let readyMessage;
if (command === "wasm") {
readyMessage = intlMsg.commands_create_readyProtocol();
} else if (command === "app") {
readyMessage = intlMsg.commands_create_readyApp();
} else if (command === "plugin") {
readyMessage = intlMsg.commands_create_readyPlugin();
}
logger.info(`🔥 ${readyMessage} 🔥`);
process.exit(0);
})
.catch((err) => {
const commandFailError = intlMsg.commands_create_error_commandFail({
error: JSON.stringify(err, null, 2),
});
logger.error(commandFailError);
process.exit(1);
try {
await generateProjectTemplate(command, language, projectDir);
let readyMessage;
if (command === "wasm") {
readyMessage = intlMsg.commands_create_readyProtocol();
} else if (command === "app") {
readyMessage = intlMsg.commands_create_readyApp();
} else if (command === "plugin") {
readyMessage = intlMsg.commands_create_readyPlugin();
}
logger.info(`🔥 ${readyMessage} 🔥`);
process.exit(0);
} catch (err) {
const commandFailError = intlMsg.commands_create_error_commandFail({
error: JSON.stringify(err, null, 2),
});
logger.error(commandFailError);
process.exit(1);
}
}
126 changes: 59 additions & 67 deletions packages/cli/src/lib/project/templates/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,87 +81,79 @@ const executeCommand = (
});
};

export const generateProjectTemplate = (
export const generateProjectTemplate = async (
type: string,
lang: string,
projectDir: string
): Promise<boolean | { command: string }> => {
return new Promise((resolve, reject) => {
let command = "";
let args: string[] = [];
let command = "";
let args: string[] = [];

const useYarn = shouldUseYarn();
const isOnline = checkIfOnline(useYarn);
const useYarn = shouldUseYarn();
const isOnline = checkIfOnline(useYarn);

const root = path.resolve(projectDir);
const dependencies: string[] = ["@polywrap/templates"];
const root = path.resolve(projectDir);
const dependencies: string[] = ["@polywrap/templates"];

fs.writeFileSync(
path.join(root, "package.json"),
`
fs.writeFileSync(
path.join(root, "package.json"),
`
{
"name": "template"
}
`
);
);

if (useYarn) {
command = "yarnpkg";
args = ["add", "--exact"];
if (useYarn) {
command = "yarnpkg";
args = ["add", "--exact"];

if (!isOnline) {
args.push("--offline");
}
if (!isOnline) {
args.push("--offline");
}

args.push(...dependencies);

// Explicitly set cwd() to work around issues like
// https://github.com/facebook/create-react-app/issues/3326.
// Unfortunately we can only do this for Yarn because npm support for
// equivalent --prefix flag doesn't help with this issue.
// This is why for npm, we run checkThatNpmCanReadCwd() early instead.
args.push("--cwd");
args.push(root);

if (!isOnline) {
const offlineMessage = intlMsg.lib_generators_projectGenerator_offline();
const fallbackMessage = intlMsg.lib_generators_projectGenerator_fallback();
console.log(chalk.yellow(offlineMessage));
console.log(chalk.yellow(fallbackMessage));
console.log();
}
} else {
command = "npm";
args = [
"install",
"--save",
"--save-exact",
"--loglevel",
"error",
].concat(dependencies);
args.push(...dependencies);

// Explicitly set cwd() to work around issues like
// https://github.com/facebook/create-react-app/issues/3326.
// Unfortunately we can only do this for Yarn because npm support for
// equivalent --prefix flag doesn't help with this issue.
// This is why for npm, we run checkThatNpmCanReadCwd() early instead.
args.push("--cwd");
args.push(root);

if (!isOnline) {
const offlineMessage = intlMsg.lib_generators_projectGenerator_offline();
const fallbackMessage = intlMsg.lib_generators_projectGenerator_fallback();
console.log(chalk.yellow(offlineMessage));
console.log(chalk.yellow(fallbackMessage));
console.log();
}
} else {
command = "npm";
args = ["install", "--save", "--save-exact", "--loglevel", "error"].concat(
dependencies
);
}

executeCommand(command, args, root)
.then(() => {
fse
.copy(
`${root}/node_modules/@polywrap/templates/${type}/${lang}`,
`${root}`,
{
overwrite: true,
}
)
.then(() => {
resolve(true);
})
.catch(() => {
reject({
command: `copy ${root}/node_modules/@polywrap/templates/${type}/${lang} ${root}`,
});
});
})
.catch((error) => {
reject(error);
});
});
try {
await executeCommand(command, args, root);
} catch (e) {
return e;
}

try {
await fse.copy(
`${root}/node_modules/@polywrap/templates/${type}/${lang}`,
`${root}`,
{
overwrite: true,
}
);
return true;
} catch (e) {
return {
command: `copy ${root}/node_modules/@polywrap/templates/${type}/${lang} ${root}`,
};
}
};
2 changes: 1 addition & 1 deletion packages/js/cli/src/__tests__/commands.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ const testData: CommandTestCaseData<CommandTypings> = {
create: {
app: [{
cwd: fs.mkdtempSync(path.join(os.tmpdir(), "cli-js-create-test")),
arguments: ["typescript-node", "test-app"],
arguments: ["typescript", "test-app"],
after: (test) => {
if (!test.cwd)
throw Error("This shouldn't happen");
Expand Down
4 changes: 2 additions & 2 deletions packages/templates/app/typescript/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "templates-app-typescript-node",
"description": "Polywrap App TypeScript Node Template",
"name": "templates-app-typescript",
"description": "Polywrap App TypeScript Template",
"private": true,
"version": "0.10.0-pre.7",
"scripts": {
Expand Down

0 comments on commit 7a349ea

Please sign in to comment.