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, init): create INPUT.json from input_schema prefills #379

Merged
merged 9 commits into from
Jul 3, 2023
3 changes: 2 additions & 1 deletion 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, enhanceReadmeWithLocalSuffix } = require('../lib/create-utils');
const { httpsGet, ensureValidActorName, getTemplateDefinition, enhanceReadmeWithLocalSuffix, createInputFromSchema } = require('../lib/create-utils');

class CreateCommand extends ApifyCommand {
async run() {
Expand Down Expand Up @@ -88,6 +88,7 @@ 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');
await createInputFromSchema(actFolderDir);

// Add localReadmeSuffix which is fetched from manifest to README.md
// The suffix contains local development instructions
Expand Down
28 changes: 28 additions & 0 deletions src/lib/create-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const { validateActorName } = require('./utils');
const {
warning,
} = require('./outputs');
const { readInputSchema } = require('./input_schema');

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

Expand Down Expand Up @@ -85,6 +86,33 @@ exports.enhanceReadmeWithLocalSuffix = async (readmePath, manifestPromise) => {
}
};

/**
* Goes to the Actor directory and creates INPUT.json file from the input schema prefills.
* @param {string} actFolderDir
*/
exports.createInputFromSchema = async (actFolderDir) => {
monkey-denky marked this conversation as resolved.
Show resolved Hide resolved
monkey-denky marked this conversation as resolved.
Show resolved Hide resolved
try {
const currentDir = process.cwd();
process.chdir(actFolderDir);
const { inputSchema } = await readInputSchema();

if (inputSchema) {
const input = Object.keys(inputSchema.properties).reduce((acc, key) => {
const { prefill } = inputSchema.properties[key];
if (prefill) acc[key] = prefill;
monkey-denky marked this conversation as resolved.
Show resolved Hide resolved
return acc;
}, {});

fs.mkdirSync('./storage/key_value_stores/default', { recursive: true });
monkey-denky marked this conversation as resolved.
Show resolved Hide resolved
fs.writeFileSync('./storage/key_value_stores/default/INPUT.json', JSON.stringify(input, null, 2));
}

process.chdir(currentDir);
monkey-denky marked this conversation as resolved.
Show resolved Hide resolved
} catch (err) {
warning(`Could not create INPUT.json file. Cause: ${err.message}`);
}
};

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