mirror of
https://github.com/LukeHagar/vercel.git
synced 2025-12-09 04:22:07 +00:00
This PR adds an optional property called `sort` to each framework so that we can change the order returned in the API. The reason this is necessary is because the order of the original array determines the precedence of framework detection. So we need another way to indicate the order of templates/examples returned from the API. In particular, we need "Next.js" to be first and "Other" to be last. I also updated the deprecated `@now/node` usage to `@vercel/node` in the API.
61 lines
1.5 KiB
TypeScript
61 lines
1.5 KiB
TypeScript
import fs from 'fs';
|
|
// @ts-ignore
|
|
import tar from 'tar-fs';
|
|
import { extract } from '../../_lib/examples/extract';
|
|
import { NowRequest, NowResponse } from '@vercel/node';
|
|
import { withApiHandler } from '../../_lib/util/with-api-handler';
|
|
|
|
const TMP_DIR = '/tmp';
|
|
|
|
function isDirectory(path: string) {
|
|
return fs.existsSync(path) && fs.lstatSync(path).isDirectory();
|
|
}
|
|
|
|
function notFound(res: NowResponse, message: string) {
|
|
return res.status(404).send({
|
|
error: {
|
|
code: 'not_found',
|
|
message,
|
|
},
|
|
});
|
|
}
|
|
|
|
function streamToBuffer(stream: any) {
|
|
return new Promise((resolve, reject) => {
|
|
const buffers: any[] = [];
|
|
stream.on('error', (err: any) => {
|
|
reject(err);
|
|
});
|
|
stream.on('data', (b: any) => {
|
|
buffers.push(b);
|
|
});
|
|
stream.on('end', () => {
|
|
resolve(Buffer.concat(buffers));
|
|
});
|
|
});
|
|
}
|
|
|
|
export default withApiHandler(async function (
|
|
req: NowRequest,
|
|
res: NowResponse
|
|
) {
|
|
const ext = '.tar.gz';
|
|
const { segment = '' } = req.query;
|
|
|
|
if (Array.isArray(segment) || !segment.endsWith(ext)) {
|
|
return notFound(res, `Missing ${ext} suffix.`);
|
|
}
|
|
|
|
const example = segment.slice(0, -ext.length);
|
|
|
|
await extract('https://github.com/vercel/vercel/archive/master.zip', TMP_DIR);
|
|
const directory = `${TMP_DIR}/vercel-master/examples/${example}`;
|
|
|
|
if (!isDirectory(directory)) {
|
|
return notFound(res, `Example '${example}' was not found.`);
|
|
}
|
|
|
|
const stream = tar.pack(directory);
|
|
return res.send(await streamToBuffer(stream));
|
|
});
|