Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(create): merge local development instructions into README #372

Merged
merged 10 commits into from
Jun 21, 2023
4 changes: 2 additions & 2 deletions npm-shrinkwrap.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 12 additions & 7 deletions src/commands/create.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ const {
detectNpmVersion,
} = require('../lib/utils');
const { EMPTY_LOCAL_CONFIG, LOCAL_CONFIG_PATH, PYTHON_VENV_PATH, SUPPORTED_NODEJS_VERSION } = require('../lib/consts');
const { httpsGet, ensureValidActorName, getTemplateDefinition } = require('../lib/create-utils');
const { httpsGet, ensureValidActorName, getTemplateDefinition, enhanceReadmeWithLocalSuffix } = require('../lib/create-utils');

class CreateCommand extends ApifyCommand {
async run() {
Expand All @@ -41,16 +41,16 @@ class CreateCommand extends ApifyCommand {

// Start fetching manifest immediately to prevent
// annoying delays that sometimes happen on CLI startup.
const manifestPromise = templateArchiveUrl
? undefined // not fetching manifest when we have direct template URL
: actorTemplates.fetchManifest().catch((err) => {
return new Error(`Could not fetch template list from server. Cause: ${err?.message}`);
});
const manifestPromise = actorTemplates.fetchManifest().catch((err) => {
return new Error(`Could not fetch template list from server. Cause: ${err?.message}`);
});

actorName = await ensureValidActorName(actorName);
let messages = null;

this.telemetryData.fromArchiveUrl = !!templateArchiveUrl;
if (manifestPromise) {

if (!templateArchiveUrl) {
const templateDefinition = await getTemplateDefinition(templateName, manifestPromise);
({ archiveUrl: templateArchiveUrl, skipOptionalDeps, messages } = templateDefinition);
this.telemetryData.templateId = templateDefinition.id;
Expand Down Expand Up @@ -87,6 +87,11 @@ class CreateCommand extends ApifyCommand {

const packageJsonPath = path.join(actFolderDir, 'package.json');
const requirementsTxtPath = path.join(actFolderDir, 'requirements.txt');
const readmePath = path.join(actFolderDir, 'README.md');

// Add localReadmeSuffix which is fetched from manifest to README.md
// The suffix contains local development instructions
await enhanceReadmeWithLocalSuffix(readmePath, manifestPromise);

let dependenciesInstalled = false;
if (!skipDependencyInstall) {
Expand Down
27 changes: 27 additions & 0 deletions src/lib/create-utils.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
const chalk = require('chalk');
const inquirer = require('inquirer');
const https = require('https');
const { pipeline } = require('stream');
const { promisify } = require('util');
const fs = require('fs');
const { validateActorName } = require('./utils');
const {
warning,
} = require('./outputs');

const PROGRAMMING_LANGUAGES = ['JavaScript', 'TypeScript', 'Python'];

Expand Down Expand Up @@ -58,6 +64,27 @@ exports.getTemplateDefinition = async (maybeTemplateName, manifestPromise) => {
return executePrompts(manifest);
};

/**
* Fetch local readme suffix from the manifest and append it to the readme.
* @param {string} readmePath
* @param {Promise<object>} manifestPromise
*/
exports.enhanceReadmeWithLocalSuffix = async (readmePath, manifestPromise) => {
const manifest = await manifestPromise;
// If the fetch failed earlier, the resolve value of
// the promise will be the error from fetching the manifest.
if (manifest instanceof Error) throw manifest;

try {
const suffixStream = await this.httpsGet(manifest.localReadmeSuffixUrl);
const readmeStream = fs.createWriteStream(readmePath, { flags: 'a' });
readmeStream.write('\n\n');
await promisify(pipeline)(suffixStream, readmeStream);
} catch (err) {
warning(`Could not append local development instructions to README.md. Cause: ${err.message}`);
}
};

/**
* Inquirer does not have a native way to "go back" between prompts.
* @param {object} manifest
Expand Down