* Extract example to directory
( client: Client, name: string, dir: string, force?: boolean, ver = 'v2' )
| 124 | * Extract example to directory |
| 125 | */ |
| 126 | async function extractExample( |
| 127 | client: Client, |
| 128 | name: string, |
| 129 | dir: string, |
| 130 | force?: boolean, |
| 131 | ver = 'v2' |
| 132 | ) { |
| 133 | const folder = prepareFolder(client.cwd, dir || name, force); |
| 134 | output.spinner(`Fetching ${name}`); |
| 135 | |
| 136 | const url = `${EXAMPLE_API}/${ver}/download/${name}.tar.gz`; |
| 137 | |
| 138 | return client |
| 139 | .fetch(url, { json: false }) |
| 140 | .then(async res => { |
| 141 | output.stopSpinner(); |
| 142 | |
| 143 | if (res.status !== 200) { |
| 144 | throw new Error(`Could not get ${name}.tar.gz`); |
| 145 | } |
| 146 | |
| 147 | await new Promise((resolve, reject) => { |
| 148 | const extractor = tar.extract(folder); |
| 149 | const body = toNodeReadable(res.body); |
| 150 | body.on('error', reject); |
| 151 | extractor.on('error', reject); |
| 152 | extractor.on('finish', resolve); |
| 153 | body.pipe(extractor); |
| 154 | }); |
| 155 | |
| 156 | const successLog = `Initialized "${chalk.bold( |
| 157 | name |
| 158 | )}" example in ${chalk.bold(toHumanPath(folder))}.`; |
| 159 | const folderRel = path.relative(client.cwd, folder); |
| 160 | const deployHint = |
| 161 | folderRel === '' |
| 162 | ? listItem(`To deploy, run ${getCommandName()}.`) |
| 163 | : listItem( |
| 164 | `To deploy, ${cmd( |
| 165 | `cd ${folderRel}` |
| 166 | )} and run ${getCommandName()}.` |
| 167 | ); |
| 168 | output.success(`${successLog}\n${deployHint}`); |
| 169 | return 0; |
| 170 | }) |
| 171 | .catch(e => { |
| 172 | output.stopSpinner(); |
| 173 | throw e; |
| 174 | }); |
| 175 | } |
| 176 | |
| 177 | /** |
| 178 | * Check & prepare destination folder for extracting. |
no test coverage detected