|
| 1 | +const shell = require("shelljs"); |
| 2 | +const path = require("path"); |
| 3 | +const fs = require("fs"); |
| 4 | +const wrapTagAround = require("configure-react/utils/wrapTagAround"); |
| 5 | +const importAtTop = require("configure-react/utils/importAtTop"); |
| 6 | + |
| 7 | +const detail = require("configure-react/commands/details.json"); |
| 8 | + |
| 9 | +const { |
| 10 | + editReadme, |
| 11 | + createDotEnv, |
| 12 | + createComponent, |
| 13 | + checkIfRoot, |
| 14 | + makeCodePritter, |
| 15 | + endingScreen, |
| 16 | +} = require("configure-react/utils"); |
| 17 | + |
| 18 | +const createAxios = (args) => { |
| 19 | + if (!checkIfRoot(args)) { |
| 20 | + return console.log( |
| 21 | + "You are not in the root of a react app. Please run this command in the root of a react app." |
| 22 | + ); |
| 23 | + } |
| 24 | + const currentPath = process.cwd(); |
| 25 | + startCreatingAxios(currentPath); |
| 26 | + endingScreen(); |
| 27 | +}; |
| 28 | + |
| 29 | +const startCreatingAxios = (currentPath) => { |
| 30 | + shell.exec("npm i axios"); |
| 31 | + |
| 32 | + const readmePath = path.join(currentPath, "./README.md"); |
| 33 | + editReadme(readmePath, "axios"); |
| 34 | + shell.mkdir("-p", path.join(currentPath, "./src/api")); // -p flag creates parent directories if they don't exist |
| 35 | + // create file if not exist |
| 36 | + const env = path.join(currentPath, "./.env"); |
| 37 | + const componentPath = path.join(currentPath, "./src/components"); |
| 38 | + // if (!fs.existsSync(env)) { |
| 39 | + createDotEnv(env); |
| 40 | + createComponent(componentPath); |
| 41 | + // } |
| 42 | + |
| 43 | + shell.touch(path.join(currentPath, "./src/api/index.js")); |
| 44 | + shell.touch(path.join(currentPath, "./src/api/login.js")); |
| 45 | + shell.touch(path.join(currentPath, "./src/api/register.js")); |
| 46 | + shell.touch(path.join(currentPath, "./src/api/getData.js")); |
| 47 | + shell.touch(path.join(currentPath, "./src/api/postData.js")); |
| 48 | + |
| 49 | + const indexFilePath = path.join(currentPath, "./src/api/index.js"); |
| 50 | + const loginFilePath = path.join(currentPath, "./src/api/login.js"); |
| 51 | + const registerFilePath = path.join(currentPath, "./src/api/register.js"); |
| 52 | + const getDataFilePath = path.join(currentPath, "./src/api/getData.js"); |
| 53 | + const postDataFilePath = path.join(currentPath, "./src/api/postData.js"); |
| 54 | + |
| 55 | + // edit index.js |
| 56 | + const apiIndex = detail.apiIndexData.join("\n"); |
| 57 | + fs.writeFileSync(indexFilePath, makeCodePritter(apiIndex), "utf8", (err) => { |
| 58 | + if (err) throw err; |
| 59 | + }); |
| 60 | + |
| 61 | + // edit login.js |
| 62 | + const loginApiData = detail.loginApiData.join("\n"); |
| 63 | + fs.writeFileSync( |
| 64 | + loginFilePath, |
| 65 | + makeCodePritter(loginApiData), |
| 66 | + "utf8", |
| 67 | + (err) => { |
| 68 | + if (err) throw err; |
| 69 | + } |
| 70 | + ); |
| 71 | + |
| 72 | + // edit register.js |
| 73 | + const registerApiData = detail.registerApiData.join("\n"); |
| 74 | + fs.writeFileSync( |
| 75 | + registerFilePath, |
| 76 | + makeCodePritter(registerApiData), |
| 77 | + "utf8", |
| 78 | + (err) => { |
| 79 | + if (err) throw err; |
| 80 | + } |
| 81 | + ); |
| 82 | + |
| 83 | + // edit getData.js |
| 84 | + const getApiData = detail.getApiData.join("\n"); |
| 85 | + fs.writeFileSync( |
| 86 | + getDataFilePath, |
| 87 | + makeCodePritter(getApiData), |
| 88 | + "utf8", |
| 89 | + (err) => { |
| 90 | + if (err) throw err; |
| 91 | + } |
| 92 | + ); |
| 93 | + |
| 94 | + // edit postData.js |
| 95 | + const postApiData = detail.postApiData.join("\n"); |
| 96 | + fs.writeFileSync( |
| 97 | + postDataFilePath, |
| 98 | + makeCodePritter(postApiData), |
| 99 | + "utf8", |
| 100 | + (err) => { |
| 101 | + if (err) throw err; |
| 102 | + } |
| 103 | + ); |
| 104 | +}; |
| 105 | + |
| 106 | +module.exports = createAxios; |
0 commit comments