Skip to content

Commit

Permalink
Replace default API key with user input
Browse files Browse the repository at this point in the history
  • Loading branch information
se030 authored and blurfx committed Nov 25, 2023
1 parent 626fb8c commit 3c5a069
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 6 deletions.
2 changes: 1 addition & 1 deletion tools/create-yorkie-app/.env
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
VITE_YORKIE_API_ADDR='https://api.yorkie.dev'
VITE_YORKIE_API_KEY='ckfvbld047aeajg7f01g'
VITE_YORKIE_API_KEY=
6 changes: 4 additions & 2 deletions tools/create-yorkie-app/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,7 @@ $ npm run dev

Open the browser and go to the URL that is printed in the terminal.

Note that you are sharing the Yorkie server with other create-yorkie-app users.
If you want to configure own local Yorkie server, refer to [this README](../../examples/README.md).
## Note

Yorkie API key or local server is necessary to run each example.
You can create and manage your projects and API keys at [Yorkie Dashboard](https://yorkie.dev/dashboard). If you want to configure your own server, refer to [this README](../../examples/README.md).
31 changes: 28 additions & 3 deletions tools/create-yorkie-app/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,23 +111,46 @@ const renameFiles: Record<string, string | undefined> = {
_gitignore: '.gitignore',
};

const apiKeyMessage = 'You can update your API key in .env';
const defaultTargetDir = 'yorkie-app';

async function init() {
const argTargetDir = formatTargetDir(argv._[0]);
const argTemplate = argv.template || argv.t;

let apiKey = '';
let targetDir = argTargetDir || defaultTargetDir;
const getProjectName = () =>
targetDir === '.' ? path.basename(path.resolve()) : targetDir;

let result: prompts.Answers<
'projectName' | 'overwrite' | 'packageName' | 'framework' | 'variant'
| 'apiKey'
| 'projectName'
| 'overwrite'
| 'packageName'
| 'framework'
| 'variant'
>;

try {
result = await prompts(
[
{
type: () => {
console.log(
'\n🔑 To run these examples, you need Yorkie API key.' +
'\nGet your API key at https://yorkie.dev/dashboard\n',
);

return 'text';
},
name: 'apiKey',
message: reset('API key:'),
initial: apiKeyMessage,
onState: ({ value }) => {
apiKey = value === apiKeyMessage ? '' : value;
},
},
{
type: argTargetDir ? null : 'text',
name: 'projectName',
Expand Down Expand Up @@ -288,13 +311,15 @@ async function init() {

const files = fs.readdirSync(templateDir);
for (const file of files.filter(
(f) => f !== 'package.json' && f !== '.env',
(f) => f !== '.env' && f !== 'package.json',
)) {
write(file);
}

const dotenvPath = path.resolve(fileURLToPath(import.meta.url), '../.env');
write('.env', fs.readFileSync(dotenvPath).toString());
const dotenvTemplate = fs.readFileSync(dotenvPath).toString();

write('.env', `${dotenvTemplate.trim()}'${apiKey}'`);

const pkg = JSON.parse(
fs.readFileSync(path.join(templateDir, `package.json`), 'utf-8'),
Expand Down

0 comments on commit 3c5a069

Please sign in to comment.