Create Mini Actor avatar
Create Mini Actor
Try for free

No credit card required

View all Actors
Create Mini Actor

Create Mini Actor

valek.josef/create-mini-actor
Try for free

No credit card required

.editorconfig

1root = true
2
3[*]
4indent_style = space
5indent_size = 4
6charset = utf-8
7trim_trailing_whitespace = true
8insert_final_newline = true
9end_of_line = lf

.eslintrc

1{
2    "extends": "@apify",
3    "root": true
4}

.gitignore

1# This file tells Git which files shouldn't be added to source control
2
3.idea
4node_modules

Dockerfile

1# First, specify the base Docker image. You can read more about
2# the available images at https://sdk.apify.com/docs/guides/docker-images
3# You can also use any other image from Docker Hub.
4FROM apify/actor-node:16
5
6# Second, copy just package.json and package-lock.json since it should be
7# the only file that affects "npm install" in the next step, to speed up the build
8COPY package*.json ./
9
10# Install NPM packages, skip optional and development dependencies to
11# keep the image small. Avoid logging too much and print the dependency
12# tree for debugging
13RUN npm --quiet set progress=false \
14 && npm install --only=prod --no-optional \
15 && echo "Installed NPM packages:" \
16 && (npm list --only=prod --no-optional --all || true) \
17 && echo "Node.js version:" \
18 && node --version \
19 && echo "NPM version:" \
20 && npm --version
21
22# Next, copy the remaining files and directories with the source code.
23# Since we do this after NPM install, quick build will be really fast
24# for most source file changes.
25COPY . ./
26
27# Optionally, specify how to launch the source code of your actor.
28# By default, Apify's base Docker images define the CMD instruction
29# that runs the Node.js source code using the command specified
30# in the "scripts.start" section of the package.json file.
31# In short, the instruction looks something like this:
32#
33# CMD npm start

INPUT_SCHEMA.json

1{
2    "title": "Input schema for the apify_project actor.",
3    "type": "object",
4    "schemaVersion": 1,
5    "properties": {
6        "miniActorName": {
7            "title": "Name of the new miniactor",
8            "type": "string",
9            "description": "Name of the actor the miniactor should be based on",
10            "editor": "textfield"
11        },
12        "targetActorIdOrFullName": {
13            "title": "Target actor ID (or full name)",
14            "type": "string",
15            "description": "Name of the actor the miniactor should be based on",
16            "editor": "textfield"
17        }
18    },
19    "required": ["targetActorIdOrFullName", "miniActorName"]
20}

apify.json

1{
2    "env": { "npm_config_loglevel": "silent" }
3}

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 properly
11        await Actor.metamorph('${targetActorId}', {...input});
12    });
13    `;
14
15    const dockerfile = `FROM apify/actor-node:16
16    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 --version
26
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 actor
10    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 build
16    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 file
22    const {inputSchema, readme} = targetBuild;
23    
24    const sourceFiles = generateSourceFiles({
25        targetActorId: targetActor.id,
26        actorName: miniActorName,
27        inputSchema,
28        readme
29    });
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 one
36        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

1{
2    "name": "project-empty",
3    "version": "0.0.1",
4    "description": "This is a boilerplate of an Apify actor.",
5    "dependencies": {
6        "apify": "^3.0.1"
7    },
8    "devDependencies": {
9        "@apify/eslint-config": "^0.3.1",
10        "eslint": "^8.20.0"
11    },
12    "scripts": {
13        "start": "node main.js",
14        "lint": "./node_modules/.bin/eslint ./src --ext .js,.jsx",
15        "lint:fix": "./node_modules/.bin/eslint ./src --ext .js,.jsx --fix",
16        "test": "echo \"Error: oops, the actor has no tests yet, sad!\" && exit 1"
17    },
18    "author": "It's not you it's me",
19    "license": "ISC"
20}
Developer
Community logoMaintained by Community
Actor metrics
  • 1 monthly users
  • Created in Aug 2022
  • Modified over 1 year ago
Categories

You might also like these Actors