Create Mini Actor
Pricing
Pay per usage
Go to Store
Create Mini Actor
0.0 (0)
Pricing
Pay per usage
4
Total users
3
Monthly users
1
Last modified
3 years ago
.editorconfig
root = true
[*]indent_style = spaceindent_size = 4charset = utf-8trim_trailing_whitespace = trueinsert_final_newline = trueend_of_line = lf
.eslintrc
{ "extends": "@apify", "root": true}
.gitignore
# This file tells Git which files shouldn't be added to source control
.ideanode_modules
Dockerfile
# First, specify the base Docker image. You can read more about# the available images at https://sdk.apify.com/docs/guides/docker-images# You can also use any other image from Docker Hub.FROM apify/actor-node:16
# Second, copy just package.json and package-lock.json since it should be# the only file that affects "npm install" in the next step, to speed up the buildCOPY package*.json ./
# Install NPM packages, skip optional and development dependencies to# keep the image small. Avoid logging too much and print the dependency# tree for debuggingRUN npm --quiet set progress=false \ && npm install --only=prod --no-optional \ && echo "Installed NPM packages:" \ && (npm list --only=prod --no-optional --all || true) \ && echo "Node.js version:" \ && node --version \ && echo "NPM version:" \ && npm --version
# Next, copy the remaining files and directories with the source code.# Since we do this after NPM install, quick build will be really fast# for most source file changes.COPY . ./
# Optionally, specify how to launch the source code of your actor.# By default, Apify's base Docker images define the CMD instruction# that runs the Node.js source code using the command specified# in the "scripts.start" section of the package.json file.# In short, the instruction looks something like this:## CMD npm start
INPUT_SCHEMA.json
{ "title": "Input schema for the apify_project actor.", "type": "object", "schemaVersion": 1, "properties": { "miniActorName": { "title": "Name of the new miniactor", "type": "string", "description": "Name of the actor the miniactor should be based on", "editor": "textfield" }, "targetActorIdOrFullName": { "title": "Target actor ID (or full name)", "type": "string", "description": "Name of the actor the miniactor should be based on", "editor": "textfield" } }, "required": ["targetActorIdOrFullName", "miniActorName"]}
apify.json
{ "env": { "npm_config_loglevel": "silent" }}
generator.js
1const generateSourceFiles = ({targetActorId, actorName, inputSchema, readme}) => {2 const mainJs = `// This is the main Node.js source code file of your actor.3
4 // Import Apify SDK. For more information, see https://sdk.apify.com/5 const { Actor } = require('apify');6
7 Actor.main(async () => {8 // Get input of the actor (here only for demonstration purposes).9 const input = await Actor.getInput();10 // TODO: Transform the input properly11 await Actor.metamorph('${targetActorId}', {...input});12 });13 `;14
15 const dockerfile = `FROM apify/actor-node:1616 COPY package*.json ./17
18 RUN npm --quiet set progress=false \19 && npm install --only=prod --no-optional \20 && echo "Installed NPM packages:" \21 && (npm list --only=prod --no-optional --all || true) \22 && echo "Node.js version:" \23 && node --version \24 && echo "NPM version:" \25 && npm --version26
27 COPY . ./28 `;29
30 const packageJson = {31 "name": actorName,32 "version": "0.0.1",33 "description": "This is a boilerplate of an Apify actor.",34 "dependencies": {35 "apify": "^3.0.1"36 },37 "devDependencies": {38 "@apify/eslint-config": "^0.3.1",39 "eslint": "^8.20.0"40 },41 "scripts": {42 "start": "node main.js",43 "lint": "./node_modules/.bin/eslint ./src --ext .js,.jsx",44 "lint:fix": "./node_modules/.bin/eslint ./src --ext .js,.jsx --fix",45 "test": "echo \"Error: oops, the actor has no tests yet, sad!\" && exit 1"46 },47 "author": "It's not you it's me",48 "license": "ISC"49 };50
51 const files = [52 {53 name: 'main.js',54 format: 'TEXT',55 content: mainJs,56 },57 {58 name: 'Dockerfile',59 format: 'TEXT',60 content: dockerfile,61 },62 {63 name: 'package.json',64 format: 'TEXT',65 content: JSON.stringify(packageJson, null, 2),66 },67 {68 name: 'INPUT_SCHEMA.json',69 format: 'TEXT',70 content: inputSchema,71 },72 {73 name: 'README.md',74 format: 'TEXT',75 content: readme,76 }77 ];78 return files;79}80
81module.exports = {82 generateSourceFiles,83}
main.js
1const { Actor } = require('apify');2const { generateSourceFiles } = require('./generator');3
4Actor.main(async () => {5 const { targetActorIdOrFullName, miniActorName } = await Actor.getInput();6
7 const client = Actor.newClient();8
9 // Fetch the target actor10 const targetActor = await client.actor(targetActorIdOrFullName).get();11 console.log('Loadad target actor', {id: targetActor.id, name: targetActor.name});12
13 const { defaultRunOptions, taggedBuilds } = targetActor;14
15 // TODO: There must be a nicer way how to get the default build16 const { buildId: targetBuildId } = taggedBuilds[defaultRunOptions.build];17 const targetBuild = await client.build(targetBuildId).get();18
19 console.log('Loaded target build', {id: targetBuild.id});20
21 // TODO: add actorDefinition as .actor/actor.json file22 const {inputSchema, readme} = targetBuild;23 24 const sourceFiles = generateSourceFiles({25 targetActorId: targetActor.id,26 actorName: miniActorName,27 inputSchema,28 readme29 });30
31 console.log('Generated source files');32
33 const miniActor = await client.actors().create({34 name: miniActorName,35 // We want to run the actor with the same options as the original one36 defaultRunOptions: {...defaultRunOptions, build: 'latest' },37 versions: [38 { 39 versionNumber: '0.0',40 sourceType: 'SOURCE_FILES',41 sourceFiles,42 buildTag: 'latest',43 }44 ],45 });46
47 console.log('Created new mini actor', miniActor);48 console.log('Building...')49 await client.actor(miniActor.id).build('0.0', { waitForFinish: 300 });50
51 console.log('Mini actor built');52
53 console.log('Mini actor was created!', {54 url: `https://console.apify.com/actors/${miniActor.id}`,55 });56
57});
package.json
{ "name": "project-empty", "version": "0.0.1", "description": "This is a boilerplate of an Apify actor.", "dependencies": { "apify": "^3.0.1" }, "devDependencies": { "@apify/eslint-config": "^0.3.1", "eslint": "^8.20.0" }, "scripts": { "start": "node main.js", "lint": "./node_modules/.bin/eslint ./src --ext .js,.jsx", "lint:fix": "./node_modules/.bin/eslint ./src --ext .js,.jsx --fix", "test": "echo \"Error: oops, the actor has no tests yet, sad!\" && exit 1" }, "author": "It's not you it's me", "license": "ISC"}